From: Norman Makoto Su Date: 2001-05-31T11:24:39+09:00 Subject: [ruby-talk:16069] KeyboardState w/ Win32Api Hello, I'm trying to use the Win32API library in ruby to force num-lock. I found the following perl snippet (which should work with perl, I think) on the net and I converted it to ruby: use Win32::API; $GetKeyboardState = new Win32::API('user32', 'GetKeyboardState', ['P'], 'N'); $SetKeyboardState = new Win32::API('user32', 'SetKeyboardState', ['P'], 'V'); my $keyboardstate = "\0" x 256; $GetKeyboardState->Call($keyboardstate); substr($keyboardstate, 0x90, 1) = chr(1); # VK_NUMLOCK = 0x90 $SetKeyboardState->Call($keyboardstate); -------ruby below---------- VK_NUMLOCK = 0x90 getKeyboardState = Win32API.new('user32', 'GetKeyboardState', ['P'], 'N') setKeyboardState = Win32API.new('user32', 'SetKeyboardState', ['P'], 'V') state = 0.chr * 256 getKeyboardState.call(state) state[VK_NUMLOCK] = 1.chr setKeyboardState.call(state) However, this does not change the num-lock; in fact, nothing at all happens (no errors either). I've tried making a keyboard state byte array when I set caps lock, num lock, or press shift down. Then, I compared that to a keyboard state where I press nothing on the keyboard but it seems that there is no difference; for some reason, the keyboard state is not recording correctly? BTW, GetKeyboardState takes in a byte array and writes to it. Do I need to unpack it somehow? Since there is no byte type in ruby-- I'm guessing character should work too... I'm using cygwin version 1.3.2 on Windows 98 (Japanese edition) and ruby version 1.6.3 [i386-cygwin]. Any help would be greatly appreciated. Thank you, Norman Su