Class: String

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

Instance Method Summary collapse

Instance Method Details

#to_keyObject

converts one-char string into keyboard-scan ‘Virtual key’ code TODO: only letters and numbers convertible so far, need to extend further



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

def to_key
  unless size == 1
    raise "Can't convert but a single character: #{self}"
  end
  ascii = upcase.unpack('C')[0]
#    puts "I'm here with #{self}->#{ascii}"
  case self
    when 'a'..'z', '0'..'9', ' '
      [ascii]
    when 'A'..'Z'
      [WinGui.const_get(:VK_SHIFT), ascii]
    when ','
      [WinGui.const_get(:VK_OEM_COMMA)]
    when '.'
      [WinGui.const_get(:VK_OEM_PERIOD)]
    when ';'
      [WinGui.const_get(:VK_OEM_1)]
    when ':'
      [:VK_SHIFT, :VK_OEM_1].map {|s| WinGui.const_get s}
    when "\\"
      [WinGui.const_get(:VK_OEM_102)]
    when "\n"
      [WinGui.const_get(:VK_RETURN)]
    else
      raise "Can't convert unknown character: #{self}"
  end
end

#to_printObject



32
33
34
# File 'lib/extension.rb', line 32

def to_print
  force_encoding('cp1251').encode(Encoding.default_external, :undef => :replace)
end