-
Notifications
You must be signed in to change notification settings - Fork 10
/
key.cr
88 lines (80 loc) · 2.93 KB
/
key.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
module CrystGLFW
# A key represents a physical key on the keyboard.
struct Key
# All of the key-specific labels defined as constants.
@@labels : Array(Symbol) = CrystGLFW.constants.keys.select { |label| label.to_s.starts_with?("key") }
# All of the key labels that represent a printable key.
@@printable_key_labels = [:key_apostrophe, :key_comma, :key_minus, :key_period, :key_slash, :key_semicolon, :key_equal,
:key_left_bracket, :key_right_bracket, :key_backslash, :key_world_1, :key_world_2, :key_0, :key_1,
:key_2, :key_3, :key_4, :key_5, :key_6, :key_7, :key_8, :key_9, :key_a, :key_b, :key_c, :key_d, :key_e,
:key_f, :key_g, :key_h, :key_i, :key_j, :key_k, :key_l, :key_m, :key_n, :key_o, :key_p, :key_q, :key_r,
:key_s, :key_t, :key_u, :key_v, :key_w, :key_x, :key_y, :key_z, :key_kp_0, :key_kp_1, :key_kp_2,
:key_kp_3, :key_kp_4, :key_kp_5, :key_kp_6, :key_kp_7, :key_kp_8, :key_kp_9, :key_kp_decimal,
:key_kp_divide, :key_kp_multiply, :key_kp_subtract, :key_kp_add, :key_kp_equal]
getter name : String | Nil
getter code : Int32
getter scancode : Int32
# :nodoc:
def initialize(code : Int32, scancode : Int32)
@code = code
@scancode = scancode
@name = key_name if printable?
end
# Returns true if the key is considered printable. False otherwise.
#
# ```
# window.on_key do |key_event|
# key = key_event.key
# puts key.name if key.printable?
# end
# ```
def printable? : Bool
label = @@printable_key_labels.find { |label| @code == CrystGLFW[label] }
!label.nil?
end
# :nodoc:
def ==(other : Key)
@scancode == other.scancode
end
# Returns true if the key is referenced by the given label. False otherwise.
#
# ```
# window.on_key do |key_event|
# key = key_event.key
# if key.is? :key_a
# puts "the 'a' key was pressed."
# end
# end
# ```
#
# Also accepts multiple labels and returns true if the key's label matches any of them.
#
# ```
# window.on_key do |key_event|
# key = key_event.key
# if key.is? :key_a, :key_b, :key_c
# puts "A, B, C!"
# elsif key.is? :key_1, :key_2, :key_3
# puts "Easy as 1, 2, 3!"
# else
# puts "Uh...do, re, mi?"
# end
# end
# ```
#
# This method accepts the following arguments:
# - *labels*, any number of labels against which the key will be checked.
def is?(*labels : Symbol) : Bool
maybe_label = labels.find { |label| @code == CrystGLFW[label] }
!maybe_label.nil?
end
# :nodoc:
def to_unsafe : Int32
@code
end
# Returns the key's name.
private def key_name : String
String.new(LibGLFW.get_key_name(@code, @scancode))
end
end
end