Class: Vbox::Scancode

Inherits:
Object
  • Object
show all
Defined in:
lib/vbox/scancode.rb

Class Method Summary collapse

Class Method Details

.send_keycode(vboxcmd, vname, keycode) ⇒ Object



39
40
41
42
43
# File 'lib/vbox/scancode.rb', line 39

def self.send_keycode(vboxcmd,vname,keycode)
  command= "#{vboxcmd} controlvm '#{vname}' keyboardputscancode #{keycode}"
  #puts "#{command}"
  IO.popen("#{command}") { |f| print '' }
end

.send_sequence(vboxcmd, vname, sequence, webport) ⇒ Object



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
# File 'lib/vbox/scancode.rb', line 4

def self.send_sequence(vboxcmd,vname,sequence,webport)
      puts
      counter=0
      sequence.each { |s|
counter=counter+1

s.gsub!(/%IP%/,Vbox::Session.local_ip);
s.gsub!(/%PORT%/,webport);
s.gsub!(/%NAME%/, vname);
puts "Typing:[#{counter}]: "+s

keycodes=string_to_keycode(s)

# VBox seems to have issues with sending the scancodes as one big
# .join()-ed string. It seems to get them out or order or ignore some.
# A workaround is to send the scancodes one-by-one.
codes=""
for keycode in keycodes.split(' ') do

    unless keycode=="wait"
      send_keycode(vboxcmd,vname,keycode)
      sleep 0.01
    else
      sleep 1
    end
  end
  #sleep after each sequence (needs to be param)
  sleep 1
  }

  puts "Done typing."
  puts

end

.string_to_keycode(thestring) ⇒ Object



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/vbox/scancode.rb', line 45

def self.string_to_keycode(thestring)

  #http://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html

  k=Hash.new
  #numericals
  k['1'] = '02 82';
  k['2'] = '03 83';
  k['3'] = '04 84';
  k['4'] = '05 85';
  k['5'] = '06 86';
  k['6'] = '07 87';
  k['7'] = '08 88';
  k['8'] = '09 89';
  k['9'] = '0a 8a';
  k['0'] = '0b 8b';

  #symbols
  k['-'] = '0c 8c';
  k['='] = '0d 8d';
  k[';'] = '27 a7';
  k['"'] = '2a 28 aa a8';
  k['\''] = '28 a8';
  k['\\'] = '2b ab';
  k['|'] = '2a 2b aa 8b';
  k['['] = '1a 9a';
  k[']'] = '1b 9b';
  k['<'] = '2a 33 aa b3';
  k['>'] = '2a 34 aa b4';
  k['?'] = '2a 35 aa b5';
  k['$'] = '2a 05 aa 85';
  k['+'] = '2a 0d aa 8d';
  k[','] = '33 b3' ;
  k['.'] = '34 b4';
  k['/'] = '35 b5' ;
  k[':'] = '2a 27 aa a7';
  k['%'] = '2a 06 aa 86';
  k['_'] = '2a 0c aa 8c';
  k['&'] = '2a 08 aa 88';
  k['('] = '2a 0a aa 8a';
  k[')'] = '2a 0b aa 8b';

  #delimiters
  k['Tab'] = '0f 8f';

  #alphabet small
  k['a'] = '1e 9e';
  k['b'] = '30 b0';
  k['c'] = '2e ae';
  k['d'] = '20 a0';
  k['e'] = '12 92';
  k['f'] = '21 a1';
  k['g'] = '22 a2';
  k['h'] = '23 a3';
  k['i'] = '17 97';
  k['j'] = '24 a4';
  k['k'] = '25 a5';
  k['l'] = '26 a6';
  k['m'] = '32 b2';
  k['n'] = '31 b1';
  k['o'] = '18 98';
  k['p'] = '19 99';
  k['q'] = '10 90';
  k['r'] = '13 93';
  k['s'] = '1f 9f';
  k['t'] = '14 94';
  k['u'] = '16 96';
  k['v'] = '2f af';
  k['w'] = '11 91';
  k['x'] = '2d ad';
  k['y'] = '15 95';
  k['z'] = '2c ac';

  #alphabet big
  k['A'] = '2a 1e aa 9e';
  k['B'] = '2a 30 aa b0';
  k['C'] = '2a 2e aa ae';
  k['D'] = '2a 20 aa a0';
  k['E'] = '2a 12 aa';
  k['F'] = '2a 21 aa a1';
  k['G'] = '2a 22 aa a2';
  k['H'] = '2a 23 aa a3';
  k['I'] = '2a 17 aa';
  k['J'] = '2a 24 aa a4';
  k['K'] = '2a 25 aa a5';
  k['L'] = '2a 26 aa a6';
  k['M'] = '2a 32 aa b2';
  k['N'] = '2a 31 aa b1';
  k['O'] = '2a 18 aa';
  k['P'] = '2a 19 aa';
  k['Q'] = '2a 10 aa';
  k['R'] = '2a 13 aa';
  k['S'] = '2a 1f aa 9f';
  k['T'] = '2a 14 aa';
  k['U'] = '2a 16 aa';
  k['V'] = '2a 2f aa af';
  k['W'] = '2a 11 aa';
  k['X'] = '2a 2d aa ad';
  k['Y'] = '2a 15 aa';
  k['Z'] = '2a 2c aa ac';

  special=Hash.new;
  special['<Enter>'] = '1c 9c';
  special['<Backspace>'] = '0e 8e';
  special['<Spacebar>'] = '39 b9';
  special['<Return>'] = '1c 9c'
  special['<Esc>'] = '01 81';
  special['<Tab>'] = '0f 8f';
  special['<KillX>'] = '1d 38 0e';
  special['<Wait>'] = 'wait';
  special['<Up>'] = '48 c8';
  special['<Down>'] = '50 d0';
  special['<PageUp>'] = '49 c9';
  special['<PageDown>'] = '51 d1';
  special['<End>'] = '4f cf';
  special['<Insert>'] = '52 d2';
  special['<Delete>'] = '53 d3';
  special['<Left>'] = '4b cb';
  special['<Right>'] = '4d cd';
  special['<Home>'] = '47 c7';

  special['<F1>'] = '3b';
  special['<F2>'] = '3c';
  special['<F3>'] = '3d';
  special['<F4>'] = '3e';
  special['<F5>'] = '3f';
  special['<F6>'] = '40';
  special['<F7>'] = '41';
  special['<F8>'] = '42';
  special['<F9>'] = '43';
  special['<F10>'] = '44';

  keycodes=''
  thestring.gsub!(/ /,"<Spacebar>")

  until thestring.length == 0
    nospecial=true;
    special.keys.each { |key|
      if thestring.start_with?(key)
        #take thestring
        #check if it starts with a special key + pop special string
        keycodes=keycodes+special[key]+' ';
        thestring=thestring.slice(key.length,thestring.length-key.length)
        nospecial=false;
        break;
      end
    }
    if nospecial
      code=k[thestring.slice(0,1)]
      if !code.nil?
        keycodes=keycodes+code+' '
      else
        puts "no scan code for #{thestring.slice(0,1)}"
      end
      #pop one
      thestring=thestring.slice(1,thestring.length-1)
    end
  end

  return keycodes
end