# Win32API keystate issue

**URL:** <https://www.ruby-forum.com/t/win32api-keystate-issue/171145>\
**Category:** Ruby\
**Created:** [June 27, 2009, 12:07am UTC](https://www.ruby-forum.com/t/win32api-keystate-issue/171145 "2009-06-27T00:07:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [June 27, 2009, 12:07am UTC](https://www.ruby-forum.com/t/win32api-keystate-issue/171145/1 "2009-06-27T00:07:48Z")

</div>

Generally speaking, I want to use:

Win32API.new(‘user32’,‘GetAsyncKeyState’,[‘i’],‘i’)

in the same way as:

Win32API.new(‘crtdll’,’\_getch’,[],‘L’).Call.chr

#########

I’m trying to take all of the user input typed into say a text document  
and modify it in realtime.

Using GetAsyncKeyState with a hash of hex characters (because  
GetAsyncKeyState only generates a 0 or 1 depending on the state of the  
key)…only allows me uppercase letters… however \_getch allows me to  
translate ascii into uppercase and lowercase.

The problem with \_getch is that it cannot be run in the background,  
hence the need to type directly into the shell… which would defeat the  
purpose of realtime manipulation of input.

Any suggestions are much appreciated.

- Mac

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [June 27, 2009, 1:13am UTC](https://www.ruby-forum.com/t/win32api-keystate-issue/171145/2 "2009-06-27T01:13:19Z")

</div>

Hi,

2009/6/27 Michael L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I’m trying to take all of the user input typed into say a text document
> 
> Any suggestions are much appreciated.

Here is a code snippet I used in the pure ruby readline.

require ‘Win32API’  
VK\_LMENU = 0xA4  
@getch = Win32API.new(“crtdll”, “\_getch”, [], ‘I’)  
@kbhit = Win32API.new(“crtdll”, “\_kbhit”, [], ‘I’)  
@GetKeyState = Win32API.new(“user32”,“GetKeyState”,[‘L’],‘L’)

if @kbhit.Call\>0  
c = @getch.Call  
alt = (@GetKeyState.call(VK\_LMENU) & 0x80) != 0  
if c==0 || c==0xE0  
r = c.chr + @getch.Call.chr  
else  
r = c.chr  
end  
r = “\e”+r if alt  
puts “#{r.inspect} pressed”  
end while true

Hope this helps,

Park H.
