PDA

View Full Version : How to get GetKeyboardState ?



efly2020
18-05-2009, 22:22
I very much greatly prefer to use GetKeyboardState for getting
the status of keys in a game.

I could not get GetKeyboardState to work using either a byte array
or a string.

Using thinBasic, I can not find a way of passing a pointer to the
external function.

:unsure:

Thanx for any help.

-- Updated file to working demo

ErosOlmi
18-05-2009, 22:53
GetKeyboardState is declared as follows:


DECLARE FUNCTION GetKeyboardState LIB "USER32.DLL" ALIAS "GetKeyboardState" (byval pbKeyState AS long) AS LONG


and it works fine as in following example:


Uses "console"

DECLARE FUNCTION GetKeyboardState LIB "USER32.DLL" ALIAS "GetKeyboardState" (byval pbKeyState AS long) AS LONG

dim KeyboardState(256) as byte
dim Counter as long
dim x, y as long

while %true
printat timer, 1, 1
printat GetKeyboardState(varptr(KeyboardState(1))), 1, 2
y = 3
x = 0
for Counter = 1 to 256
if mod(Counter, 80) = 0 then
Y += 1
x = 0
end if
x += 1
printat KeyboardState(Counter), x, y
next
wend

You can pass to it the VARPTR (pointer to a variable) to first element if using a numeric BYTE array
or
VARPTR to a fixed string if using fixed strings
or
STRPTR if using a dynamic string

The problem with GetKeyboardState function is that it return keyboard state only once unless you clear all keyboard messages from the keyboard buffer. You can see this in function documentation at: http://msdn.microsoft.com/en-us/library/ms646299(VS.85).aspx

Remarks:
An application can call this function to retrieve the current status of all the virtual keys. The status changes as a thread removes keyboard messages from its message queue. The status does not change as keyboard messages are posted to the thread's message queue, nor does it change as keyboard messages are posted to or retrieved from message queues of other threads. (Exception: Threads that are connected through AttachThreadInput share the same keyboard state.)


I'm trying to find a way to clear keyboard messages in one go in order to be able to call this function more than one time.

Eros

efly2020
18-05-2009, 23:48
Thanks!

I do not know how tbgl_GetWindowKeyState works internally and
GetKeyboardState always worked best for me when using other
programming languages, and tbgl_GetWindowKeyState did not work
so well many months ago, but now it seems to work as well or better
than GetKeyboardState when both are tested in the demo.

Also, the states in the KeyboardState array from the
call to GetKeyboardState are off by one index but this
is compensated for in the updated demo.

:escribe:

ErosOlmi
19-05-2009, 00:06
Check this piece of code and see if it can do what you need:


Uses "console"
uses "UI"

dim KeyboardState(256) as byte
dim Counter as long
dim x, y as long

dim tMsg as tagMSG
dim T0 as double = timer
dim Looper as long

while KeyboardState(%VK_ESCAPE) = 0
Looper += 1
printat "Press any key ..." & Looper, 1, 1
printat format$(timer - T0, "#0.000"), 1, 2

MyKeyState(KeyboardState)

printat " 1 2 3", 1, 3
printat "12345678901234567890123456789012", 1, 4
y = 6
x = 1
for Counter = 1 to 256
if KeyboardState(Counter) = 0 then
printat KeyboardState(Counter), x, y, 8
else
printat KeyboardState(Counter), x, y, 12
end if

if mod(Counter, 32) = 0 then
PRINTAT FORMAT$(32 * COUNTER/32, "000"), X + 5, Y, 7
Y += 1
x = 0
end if
x += 1

next

SLEEP 100
wend

'---
'---I suppose we can set this as native thinBasic function in future
'---
function MyKeyState(pByte() as byte)
static Counter as long
for Counter = 1 to 255
pByte(Counter) = GetAsyncKeyState( Counter )
next
end function

efly2020
19-05-2009, 06:14
Your demo is very good.

The least external calls to user32.dll is the goal and
that is why using one call that fills 256 bytes is better
than a few separate calls to GetAsyncKeyState.

For me, using GetAsyncKeyState has always caused
a sluggish response on older 10+ years computers,
but it should be considered older OSes are no longer
supported.

No need to "set this as native thinBasic function in future".
tbgl_GetWindowKeyState is working great and I now
know how to access GetKeyboardState if I need to
use it.
:)
Much thanks.
Ralph Flye