Class: Plushie::KeyModifiers
- Inherits:
-
Object
- Object
- Plushie::KeyModifiers
- Defined in:
- lib/plushie/key_modifiers.rb
Overview
Keyboard modifier state at the time of a key event.
Each field is a boolean indicating whether that modifier was held. Wraps a hash of modifier flags with query methods.
== Fields
- +ctrl+ -- Control key (Ctrl on Windows/Linux).
- +shift+ -- Shift key.
- +alt+ -- Alt key (Option on macOS).
- +logo+ -- Logo/Super key (Windows key, Command symbol on macOS).
- +command+ -- Platform command key (Ctrl on Windows/Linux, Cmd on macOS).
Defined Under Namespace
Classes: Mods
Class Method Summary collapse
-
.from_hash(hash) ⇒ KeyModifiers
Create from a hash (e.g. from decoded event data).
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
private
Equality comparison.
-
#alt? ⇒ Boolean
Returns true if the Alt modifier is active.
-
#command? ⇒ Boolean
Returns true if the platform Command modifier is active.
-
#ctrl? ⇒ Boolean
Returns true if the Ctrl modifier is active.
-
#hash ⇒ Object
private
Hash code for use as Hash key.
-
#initialize(ctrl: false, shift: false, alt: false, logo: false, command: false) ⇒ KeyModifiers
constructor
Create a new KeyModifiers with the given flags.
-
#inspect ⇒ Object
private
Human-readable representation.
-
#logo? ⇒ Boolean
Returns true if the Logo/Super modifier is active.
-
#shift? ⇒ Boolean
Returns true if the Shift modifier is active.
-
#to_h ⇒ Hash
Return the modifiers as a hash.
Constructor Details
#initialize(ctrl: false, shift: false, alt: false, logo: false, command: false) ⇒ KeyModifiers
Create a new KeyModifiers with the given flags. All flags default to false.
37 38 39 |
# File 'lib/plushie/key_modifiers.rb', line 37 def initialize(ctrl: false, shift: false, alt: false, logo: false, command: false) @mods = Mods.new(ctrl: ctrl, shift: shift, alt: alt, logo: logo, command: command) end |
Class Method Details
.from_hash(hash) ⇒ KeyModifiers
Create from a hash (e.g. from decoded event data).
45 46 47 48 49 50 51 52 53 |
# File 'lib/plushie/key_modifiers.rb', line 45 def self.from_hash(hash) new( ctrl: !!hash[:ctrl], shift: !!hash[:shift], alt: !!hash[:alt], logo: !!hash[:logo], command: !!hash[:command] ) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Equality comparison.
81 82 83 |
# File 'lib/plushie/key_modifiers.rb', line 81 def ==(other) other.is_a?(KeyModifiers) && to_h == other.to_h end |
#alt? ⇒ Boolean
Returns true if the Alt modifier is active.
65 |
# File 'lib/plushie/key_modifiers.rb', line 65 def alt? = @mods.alt |
#command? ⇒ Boolean
Returns true if the platform Command modifier is active.
73 |
# File 'lib/plushie/key_modifiers.rb', line 73 def command? = @mods.command |
#ctrl? ⇒ Boolean
Returns true if the Ctrl modifier is active.
57 |
# File 'lib/plushie/key_modifiers.rb', line 57 def ctrl? = @mods.ctrl |
#hash ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Hash code for use as Hash key.
88 89 90 |
# File 'lib/plushie/key_modifiers.rb', line 88 def hash to_h.hash end |
#inspect ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Human-readable representation.
94 95 96 97 |
# File 'lib/plushie/key_modifiers.rb', line 94 def inspect flags = to_h.select { |_, v| v }.keys.join(", ") "#<Plushie::KeyModifiers #{flags.empty? ? "(none)" : flags}>" end |
#logo? ⇒ Boolean
Returns true if the Logo/Super modifier is active.
69 |
# File 'lib/plushie/key_modifiers.rb', line 69 def logo? = @mods.logo |
#shift? ⇒ Boolean
Returns true if the Shift modifier is active.
61 |
# File 'lib/plushie/key_modifiers.rb', line 61 def shift? = @mods.shift |
#to_h ⇒ Hash
Return the modifiers as a hash.
77 |
# File 'lib/plushie/key_modifiers.rb', line 77 def to_h = @mods.to_h |