WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Detect Keyboard Tricks

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Detect if a Key is Pressed

Keywords:     trapkeys iskeydown waitforkey

The following sample code can detect if any key on the keyboard has been pressed:
#definefunction GetKeyboardState(buf)
	sDLLName = StrCat(DirWindows(1), "user32.dll")
	DLLCall(sDLLName, long:"GetKeyboardState", lpbinary:buf)
	BinaryEodSet(buf, 256) 
	return buf
#endfunction

boxopen("","Press any key.")
buf1 = binaryalloc(256)
before = GetKeyboardState(buf1)

buf2=binaryalloc(256)
while 1
	after = GetKeyboardState(buf2)
	if BinaryCompare(before, 0, after, 0, 256)==@false then break
endwhile
binaryfree(buf2)

message("","you pressed a key")

Article ID:   W15282
File Created: 2002:11:15:12:47:32
Last Updated: 2002:11:15:12:47:32