Added a proc to deal with keys to `Window`
[nfoiled.git] / lib / nfoiled / key.rb
blob6b6d977eb70d94f6abce2b17a9de3be7a13fc2b0
1 module Nfoiled
2   ##
3   # This is the class of a single character of input received by Nfoiled.
4   # Handles modifiers for you.
5   class Key
6     ASCII = {       :nul =>   0,  :soh =>   1,  :stx =>   2,  :etx =>   3,
7                     :'\000'=> 0,  :'\001'=> 1,  :'\002'=> 2,  :'\003'=> 3,
8       :eot =>   4,  :enq =>   5,  :ack =>   6,  :bel =>   7,  :bs  =>   8,
9       :'\004'=> 4,  :'\005'=> 5,  :'\006'=> 6,  :'\007'=> 7,  :'\b'  => 8,
10       :tab =>   9,  :nl  =>  10,  :vt  =>  11,  :np  =>  12,  :cr  =>  13,
11       :'        '  => 9,  :'\010'=>10,  :'\v'  =>11,  :'\012'=>12,  :'\013'=>13,
12       :so  =>  14,  :si  =>  15,  :dle =>  16,  :dc1 =>  17,  :dc2 =>  18,
13       :'\014'=>14,  :'\015'=>15,  :'\016'=>16,  :'\017'=>17,  :'\018'=>18,
14       :dc3 =>  19,  :dc4 =>  20,  :nak =>  21,  :syn =>  22,  :etb =>  23,
15       :'\019'=>19,  :'\020'=>20,  :'\021'=>21,  :'\022'=>22,  :'\023'=>23,
16       :can =>  24,  :em  =>  25,  :sub =>  26,  :esc =>  27,  :fs  =>  28,
17       :'\024'=>24,  :'\025'=>25,  :'\026'=>26,  :'\027'=>27,  :'\028'=>28,
18       :gs  =>  29,  :rs  =>  30,  :us  =>  31,  :sp  =>  32,
19       :'\029'=>29,  :'\030'=>30,  :'\031'=>31,  :' '   =>32,  :'!'   =>33,
20       :'"' =>  34,  :'#' =>  35,  :'$' =>  36,  :%   =>  37,  :&   =>  38,
21       :"'" =>  39,  :'(' =>  40,  :')' =>  41,  :*   =>  42,  :+   =>  43,
22       :',' =>  44,  :-   =>  45,  :'.' =>  46,  :/   =>  47,  :'0' =>  48,
23       :'1' =>  49,  :'2' =>  50,  :'3' =>  51,  :'4' =>  52,  :'5' =>  53,
24       :'6' =>  54,  :'7' =>  55,  :'8' =>  56,  :'9' =>  57,  :':' =>  58,
25       :';' =>  59,  :<   =>  60,  :'=' =>  61,  :>   =>  62,  :'?' =>  63,
26       :'@' =>  64,  :A   =>  65,  :B   =>  66,  :C   =>  67,  :D   =>  68,
27       :E   =>  69,  :F   =>  70,  :G   =>  71,  :H   =>  72,  :I   =>  73,
28       :J   =>  74,  :K   =>  75,  :L   =>  76,  :M   =>  77,  :N   =>  78,
29       :O   =>  79,  :P   =>  80,  :Q   =>  81,  :R   =>  82,  :S   =>  83,
30       :T   =>  84,  :U   =>  85,  :V   =>  86,  :W   =>  87,  :X   =>  88,
31       :Y   =>  89,  :Z   =>  90,  :'[' =>  91,  :'\\'=>  92,  :']' =>  93,
32       :^   =>  94,  :_   =>  95,  :`   =>  96,  :a   =>  97,  :b   =>  98,
33       :c   =>  99,  :d   => 100,  :e   => 101,  :f   => 102,  :g   => 103,
34       :h   => 104,  :i   => 105,  :j   => 106,  :k   => 107,  :l   => 108,
35       :m   => 109,  :n   => 110,  :o   => 111,  :p   => 112,  :q   => 113,
36       :r   => 114,  :s   => 115,  :t   => 116,  :u   => 117,  :v   => 118,
37       :w   => 119,  :x   => 120,  :y   => 121,  :z   => 122,  :'{' => 123,
38       :|   => 124,  :'}' => 125,  :~   => 126,  :del => 127}
39     
40     # The character corresponding to this keypress
41     attr_reader :char
42     
43     # Any modifier keys associating this keypress
44     attr_reader :modifiers
45     
46     ##
47     # Creates a new `Key`, including any modifiers.
48     def initialize char, opts = Hash.new
49       { :modifiers => [] }.merge opts
50       @modifiers = opts[:modifiers]
51       @char = char.to_sym
52     end
53   end
54 end