Class: Context::Key

Inherits:
Object show all
Defined in:
lib/Context/Key.rb

Overview

Represents a keypress. Each key is represented by a character and a modifier (e.g., the control key)

One could argue that this doesn’t deserve to be here at all. It is possible that some UI types won’t have a keyboard at all. However given that most will operate the same way indicates that it should have some level of abstraction. I have a feeling that this is YAGNI, though. And as such it might disappear at any point.

Direct Known Subclasses

Gtk::Key

Defined Under Namespace

Classes: Modifier

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(modifier, character) ⇒ Key

Returns a new instance of Key.



26
27
28
29
# File 'lib/Context/Key.rb', line 26

def initialize(modifier, character)
	@modifier = modifier
	@character = character
end

Instance Attribute Details

#characterObject (readonly)

Returns the value of attribute character.



24
25
26
# File 'lib/Context/Key.rb', line 24

def character
  @character
end

#modifierObject (readonly)

Returns the value of attribute modifier.



24
25
26
# File 'lib/Context/Key.rb', line 24

def modifier
  @modifier
end

Class Method Details

.nullObject

A non key that can be used as an error condition. Note: Perhaps this should be a constant??? Currently key.eql?(Key.null) will work, but key == Key.null won’t. FIXME



35
36
37
# File 'lib/Context/Key.rb', line 35

def Key.null
	Key.new(Modifier.NONE, '\0')
end

Instance Method Details

#eql?(key) ⇒ Boolean

Returns true if the two keys have the same character and modifier.

Returns:

  • (Boolean)


40
41
42
# File 'lib/Context/Key.rb', line 40

def eql?(key)
	(@modifier == key.modifier) && (@character == key.character)
end

#hashObject

Creates a hash for the key. This is needed for the KeyMap which is implemented as a hash.



46
47
48
49
50
# File 'lib/Context/Key.rb', line 46

def hash
	hashValue = @modifier.to_s
	hashValue += @character
	hashValue.hash
end

#null?Boolean

Returns true if the key is equal to Key.null.

Returns:

  • (Boolean)


53
54
55
# File 'lib/Context/Key.rb', line 53

def null?
	eql?(Key.null)
end