Class: Plushie::KeyModifiers

Inherits:
Object
  • Object
show all
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).

Examples:

mods = Plushie::KeyModifiers.new(ctrl: true, shift: false)
mods.ctrl?    #=> true
mods.shift?   #=> false
mods.command?  #=> false

Defined Under Namespace

Classes: Mods

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • ctrl (Boolean) (defaults to: false)
  • shift (Boolean) (defaults to: false)
  • alt (Boolean) (defaults to: false)
  • logo (Boolean) (defaults to: false)
  • command (Boolean) (defaults 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: , command: command)
end

Class Method Details

.from_hash(hash) ⇒ KeyModifiers

Create from a hash (e.g. from decoded event data).

Parameters:

  • hash (Hash)

    modifier flags

Returns:



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.

Returns:

  • (Boolean)


65
# File 'lib/plushie/key_modifiers.rb', line 65

def alt? = @mods.alt

#command?Boolean

Returns true if the platform Command modifier is active.

Returns:

  • (Boolean)


73
# File 'lib/plushie/key_modifiers.rb', line 73

def command? = @mods.command

#ctrl?Boolean

Returns true if the Ctrl modifier is active.

Returns:

  • (Boolean)


57
# File 'lib/plushie/key_modifiers.rb', line 57

def ctrl? = @mods.ctrl

#hashObject

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

#inspectObject

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.

Returns:

  • (Boolean)


69
# File 'lib/plushie/key_modifiers.rb', line 69

def logo? = @mods.

#shift?Boolean

Returns true if the Shift modifier is active.

Returns:

  • (Boolean)


61
# File 'lib/plushie/key_modifiers.rb', line 61

def shift? = @mods.shift

#to_hHash

Return the modifiers as a hash.

Returns:

  • (Hash)


77
# File 'lib/plushie/key_modifiers.rb', line 77

def to_h = @mods.to_h