Skip to content

Commit ef00a96

Browse files
committed
Make @text_keys available in handle_keyboard and write up docs on special keyboard handling
1 parent 474fa58 commit ef00a96

6 files changed

Lines changed: 37 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# v0.0.23 - 16 February 2025
2+
3+
* Made `@text_keys` instance variable instead of local
4+
* Wrote up notes on special keyboard handling with filtering example
5+
16
# v0.0.22 - 9 February 2025
27

38
* Fixed a bug when using `max_length` where `selection_end` (and `selection_start`) where incorrectly set (thanks to @TheCire for reporting in Discord).

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,31 @@ The argument list below will list `prompt_color` but not the individual `prompt_
148148
* `#focussed?` - Returns true if the input is focussed, false otherwise
149149
* `#value_changed?` - Returns true if the input value changed in the last tick, false otherwise
150150

151+
## Special keyboard handling
152+
153+
There may be cases where you want to do some special keyboard handling, like filtering out certain characters. In order to do this, create a subclass of `Input::Text` or `Input::Multiline` and override the `handle_keyboard` method, calling `super` when your special handling is done. The following instance variables are available:
154+
155+
* `@meta` - true if the Meta key is down (the Command key on a Mac, or Windows key on Windows)
156+
* `@alt` - true if the Alt key is down
157+
* `@shift` - true if the Shift key is down or `shift_lock` is set
158+
* `@ctrl` - true if the Control key is down
159+
* `@down_keys` - an `Array` of `Symbol`s of keys that are down excluding the special keys above
160+
* `@text_keys` - an `Array` of printable characters that has been typed this tick
161+
162+
### Example: filtering
163+
164+
``` ruby
165+
class FilenameInput < Input::Text
166+
ALLOWED_CHARS = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a + ['_', '-', '.']
167+
168+
def handle_keyboard
169+
@text_keys.select! { |key| ALLOWED_CHARS.include?(key) }
170+
171+
super
172+
end
173+
end
174+
```
175+
151176
## Console replacement
152177

153178
This library includes the ability to replace the default DragonRuby Console (`~`). Simply call `Input.replace_console!` once to enable this.

lib/keyboard.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def prepare_special_keys # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
3131
@alt = (special_keys & ALT_KEYS).any?
3232
@shift = @shift_lock || (special_keys & SHIFT_KEYS).any?
3333
@ctrl = (special_keys & CTRL_KEYS).any?
34+
35+
@text_keys = $args.inputs.text
3436
end
3537
end
3638
end

lib/multiline.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ def draw_override(ffi)
6565
end
6666

6767
def handle_keyboard
68-
text_keys = $args.inputs.text
6968
# On a Mac:
7069
# Home is Cmd + ↑ / Fn + ←
7170
# End is Cmd + ↓ / Fn + →
@@ -103,7 +102,7 @@ def handle_keyboard
103102
else
104103
@on_unhandled_key.call(@down_keys.first, self)
105104
end
106-
elsif text_keys.empty?
105+
elsif @text_keys.empty?
107106
if @down_keys.include?(:delete)
108107
delete_forward unless @readonly
109108
@ensure_cursor_visible = true
@@ -164,7 +163,7 @@ def handle_keyboard
164163
@on_unhandled_key.call(@down_keys.first, self)
165164
end
166165
else
167-
insert(text_keys.join('')) unless @readonly
166+
insert(@text_keys.join('')) unless @readonly
168167
@ensure_cursor_visible = true
169168
end
170169
end

lib/text.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ def draw_override(ffi)
2929
end
3030

3131
def handle_keyboard
32-
text_keys = $args.inputs.text
33-
3432
if @meta || @ctrl
3533
# TODO: undo/redo
3634
if @down_keys.include?(:a)
@@ -57,7 +55,7 @@ def handle_keyboard
5755
else
5856
@on_unhandled_key.call(@down_keys.first, self)
5957
end
60-
elsif text_keys.empty?
58+
elsif @text_keys.empty?
6159
if @down_keys.include?(:delete)
6260
delete_forward unless @readonly
6361
elsif @down_keys.include?(:backspace)
@@ -88,7 +86,7 @@ def handle_keyboard
8886
@on_unhandled_key.call(@down_keys.first, self)
8987
end
9088
else
91-
insert(text_keys.join('')) unless @readonly
89+
insert(@text_keys.join('')) unless @readonly
9290
@ensure_cursor_visible = true
9391
end
9492
end

metadata/game_metadata.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ devid=fascinationworks
33
devtitle=Fascination Works
44
gameid=dr-input
55
gametitle=Dragon Ruby Input
6-
version=0.0.22
6+
version=0.0.23
77
icon=metadata/icon.png
88

99
# === Flags available at all licensing tiers ===

0 commit comments

Comments
 (0)