Author |
Topic: Detecting a key release? (Read 363 times) |
|
SarmedNafi
Junior Member
member is offline


Posts: 93
|
 |
Re: Detecting a key release?
« Reply #17 on: Feb 6th, 2018, 1:24pm » |
|
Anatoly,
Here is the demo,
Code:
REM Keypress Key Release Demo By Sarmed Nafi'
REM Keys and Shift + Key
REM Keys F2 F3 Up Down Left Right Shift + Left Shift + Right
REM Works with LBB better than LB
timer 1, [loop]
[loop]
scan
if (keyState(_VK_F2 )=1) AND (F2=0) then ' >> >> F2
F2=1
PRINT "F2 pressed" : PRINT
else
if (keyState(_VK_F2 )=0) AND (F2=1) then 'Reset
F2=0
PRINT "F2 released" : PRINT
else
if (keyState(_VK_F3 )=1) AND (F3=0) then ' >> >> F3
F3=1
PRINT "F3 pressed" : PRINT
else
if (keyState(_VK_F3 )=0) AND (F3=1) then 'Reset
F3=0
PRINT "F3 released" : PRINT
else
if (keyState(_VK_UP )=1) AND (up=0) AND (Fhndl=hDate) then ' >> >> UP
up=1
PRINT "Up Arrow pressed" : PRINT
else
if (keyState(_VK_UP )=0) AND (up=1) then 'Reset
up=0
PRINT "Up Arrow released" : PRINT
else
if (keyState(_VK_DOWN )=1) AND (dwn=0) AND (Fhndl=hDate) then ' >> >> Dwn
dwn=1
PRINT "Down Arrow pressed" : PRINT
else
if (keyState(_VK_DOWN )=0) AND (dwn=1) then 'Reset
dwn=0
PRINT "Down Arrow released" : PRINT
else
if (keyState(_VK_RIGHT )=1) AND (Rt=0) AND (keyState(_VK_SHIFT )=0) then ' >> >> Right
Rt=1
PRINT "Right Arrow pressed" : PRINT
else
if (keyState(_VK_RIGHT )=0) AND (Rt=1) AND (keyState(_VK_SHIFT )=0) then 'Reset
Rt=0
PRINT "Right Arrow released" : PRINT
else
if (keyState(_VK_RIGHT )=1) AND (shRt=0) AND (keyState(_VK_SHIFT )=1) then ' >> >> Shift + Right
shRt=1
PRINT "Shift + Right Arrow pressed" : PRINT
else
if (keyState(_VK_RIGHT )=0) AND (shRt=1) AND (keyState(_VK_SHIFT )=1) then 'Reset
shRt=0
PRINT "Shift + Right Arrow released" : PRINT
else
if (keyState(_VK_LEFT )=1) AND (lft=0) AND (keyState(_VK_SHIFT )=0) AND Grd=0 then ' >> >> Left
lft=1
PRINT "Left Arrow pressed" : PRINT
else
if (keyState(_VK_LEFT )=0) AND (lft=1) AND (keyState(_VK_SHIFT )=0) then 'Reset
lft=0
PRINT "Left Arrow released" : PRINT
else
if (keyState(_VK_LEFT )=1) AND (shlft=0) AND (keyState(_VK_SHIFT )=1) AND Grd=0 then ' >> >> Shift + Left
shlft=1
PRINT "Shift + Left Arrow pressed" : PRINT
else
if (keyState(_VK_LEFT )=0) AND (shlft=1) AND (keyState(_VK_SHIFT )=1) then 'Reset
shlft=0
PRINT "Shift + Left Arrow released" : PRINT
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
WAIT
'------
function keyState(keycode)
calldll #user32, "GetAsyncKeyState", keycode AS Ulong, state AS short
if state<0 then
keyState = 1
else
keyState = 0
end if
end function
'------
The code works under LBB better than LB.
Regards, Sarmed.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
Re: Detecting a key release?
« Reply #18 on: Feb 6th, 2018, 2:58pm » |
|
on Feb 6th, 2018, 1:24pm, SarmedNafi wrote:| The code works under LBB better than LB. |
|
That code uses polling. This thread has been about the triggering (or not) of an event when a key is pressed or released.
Polling in a timer handler, as you do, and then calling the 'event' handler accordingly can be an acceptable workaround, but as Rod said it's not always a very satisfactory approach. For example it ties up the timer and, potentially, has a much greater latency than a true event.
Richard.
|
|
Logged
|
|
|
|
SarmedNafi
Junior Member
member is offline


Posts: 93
|
 |
Re: Detecting a key release?
« Reply #19 on: Feb 6th, 2018, 9:38pm » |
|
I remember that days I worked hard to find a way using INKEY$, unfortunately, INKEY$ is a simple command. I think VB6 has the same mechanism inside its engine or similar to that I used to trap keyboard event.
Quote:but as Rod said it's not always a very satisfactory approach. |
|
Rod said that because he knows TIMER is broken under Liberty Basic. I was made a very large search to discover how that big 3d games written under LB using TIMER command. The shock was they don't use the TIMER at all. If anyone uses my code under LB he will put himself in trouble and be wasting his time. Then he will find there is no way but to use LBB. When we were kids at sixties we advised each other "Don't ride a bicycle from Japan ride a bicycle from ENGLAND", It seems nothing change.
Sarmed
|
| « Last Edit: Feb 6th, 2018, 10:11pm by SarmedNafi » |
Logged
|
|
|
|
Monkfish
Full Member
member is offline


Gender: 
Posts: 104
|
 |
Re: Detecting a key release?
« Reply #20 on: Feb 7th, 2018, 09:32am » |
|
Thanks guys, and thanks Rob for clearing up exactly how Inkey$ works. 
Btw, I haven't looked at LB for a while. I only use LBB. Thanks Richard for still being here for us
|
|
Logged
|
|
|
|
|