LB Booster
« Detecting a key release? »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 03:26am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 hotthread  Author  Topic: Detecting a key release?  (Read 354 times)
Monkfish
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 104
xx Detecting a key release?
« Thread started on: Feb 4th, 2018, 1:53pm »

Hi all. I have an event-driven program that hangs around at a WAIT command waiting for an event (key press, mouse move, etc). I need to detect when the SHIFT key is released as an event. Any ideas?

Actually I may have thought of a solution as I type this. As I know the last known state of the SHIFT, and if this happens to be down, then if I detect the SHIFT key being pressed as an event it must have been released sometime during the waiting period. Might work.
« Last Edit: Feb 4th, 2018, 2:49pm by Monkfish » User IP Logged

Rod
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 110
xx Re: Detecting a key release?
« Reply #1 on: Feb 4th, 2018, 7:17pm »

LB can detect the shift release but not LBB I tried a timer and getasynckeystate but it isn't pretty.

Perhaps some more meat on the bones about the key detection strategy?
User IP Logged

Monkfish
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 104
xx Re: Detecting a key release?
« Reply #2 on: Feb 4th, 2018, 8:52pm »

Thanks Rob. Luckily my solution worked. It doesn't tell me exactly when the SHIFT key is released, just that it has been released since the last event. That is good enough for my purposes. grin
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Detecting a key release?
« Reply #3 on: Feb 4th, 2018, 9:24pm »

on Feb 4th, 2018, 7:17pm, Rod wrote:
LB can detect the shift release but not LBB

The LB Help file says of the characterInput event: "a key was pressed while the graphics window has input focus". The description of Inkey$ also refers consistently to a key having been pressed. I had no idea that an event was ever generated when a key was released, and nobody has ever mentioned that before. shocked

So is this a bug in LB 4 or an undocumented feature (or indeed is it documented somewhere that I haven't found)? If it's something that is expected, I ideally ought to modify LBB to support it, although it would not be trivial because currently key release events are completely ignored.

Richard.
User IP Logged

SarmedNafi
Junior Member
ImageImage


member is offline

Avatar




PM


Posts: 93
xx Re: Detecting a key release?
« Reply #4 on: Feb 5th, 2018, 01:25am »

A Strange talking I just read from Rob.
I have used release key event in my code, it's worked fine with LBB.
By the way, LBB timer is the best timer I ever saw. While LB timer is the worse ever.
I said that in the moment I love LB also I respect Rod very much.

Regards
User IP Logged

tsh73
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 210
xx Re: Detecting a key release?
« Reply #5 on: Feb 5th, 2018, 05:23am »

Hello SarmedNafi.
Please post your code for release key event.

User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Detecting a key release?
« Reply #6 on: Feb 5th, 2018, 08:49am »

on Feb 5th, 2018, 01:25am, SarmedNafi wrote:
I have used release key event in my code, it's worked fine with LBB.

No it doesn't, Rod is right. Indeed I have looked at the code of LBB, and - as I confirmed - key release events (technically the WM_KEYUP message) are completely ignored.

LB 4 seems to create an event from the release of the Shift and Ctrl keys, but no others that I have noticed. Although it would be possible to modify LBB to do the same, it would not be straightforward.

I wonder if it is happening 'by accident' in LB, particularly as it is undocumented, but I can't think of an obvious mechanism.

Richard.
User IP Logged

Monkfish
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 104
xx Re: Detecting a key release?
« Reply #7 on: Feb 5th, 2018, 11:37am »

I'm not doing anything special, just detecting how many times the UP_ARROW key is pressed while the SHIFT key is being held down. Something like this...

Code:
	print #main.gfx, "when characterInput [keypressed]"
'otherstuff
	wait

'check if shift key or up_arrow pressed
[keypressed]
	key$ = left$(Inkey$,2): a$ = right$(key$,1)
	if len(key$) = 2 then
		if a$ = chr$(_VK_SHIFT) then keycount = 0
		if a$ = chr$(_VK_UP) then goto [up_pressed]
	end if
'other stuff
	wait

[up_pressed]
	calldll #user32,"GetKeyState", _VK_SHIFT as long, r as long
	if (r and 32768) then keycount = keycount + 1
'other stuff
	wait
 
User IP Logged

Rod
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 110
xx Re: Detecting a key release?
« Reply #8 on: Feb 5th, 2018, 4:23pm »

If you want to react to Shift Up Arrow then Inkey$ should contain chr$(4)+chr$(38). It will be chr$(0)+chr$(38) if the shift key is not down.

I would not bother about key releases. You can always check the keystate or check for the specific combination as above.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Detecting a key release?
« Reply #9 on: Feb 5th, 2018, 4:34pm »

on Feb 5th, 2018, 11:37am, Monkfish wrote:
I'm not doing anything special, just detecting how many times the UP_ARROW key is pressed while the SHIFT key is being held down.

Testing for Shift+UpArrow is surely just a case of comparing Inkey$ with chr$(4)+chr$(38):

Code:
    open "Test" for graphics as #main
    print #main, "when characterInput [keypressed]"
'otherstuff
    wait

'check if shift + up_arrow pressed
[keypressed]
    if Inkey$ = chr$(4) + chr$(38) then
        keycount = keycount + 1
        print keycount
    end if
    wait 

I don't see the need to use an API call to test the state of the Shift key when that information is conveyed in Inkey$ anyway. Am I missing something?

Richard.
User IP Logged

Monkfish
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 104
xx Re: Detecting a key release?
« Reply #10 on: Feb 5th, 2018, 9:07pm »

That's why I rarely post any of my code... it's just an embarrassment rolleyes

Thanks guys smiley
User IP Logged

Rod
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 110
xx Re: Detecting a key release?
« Reply #11 on: Feb 5th, 2018, 9:19pm »

Slight tweak to increase keycount if sequence of shift / up arrow else zero keycount.

After putting my ipad aside I see it also needed code to handle the stream of shift key presses from auto repeat.

Ahh defeated again! it is the final shift up message that makes my code work in LB but it fails to zero the counter in LBB till another key is pressed.


Code:
open "Test" for graphics as #main
#main "when characterInput [keypressed]"
#main "setfocus"

'otherstuff
wait

'check if shift + up_arrow pressed
[keypressed]
if Inkey$ = chr$(4) + chr$(38) then
    keycount = keycount + 1
else
    if Inkey$ <> chr$(4)+chr$(16) then
        keycount = 0
    end if
end if
print keycount
wait
 
« Last Edit: Feb 6th, 2018, 07:38am by Rod » User IP Logged

Monkfish
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 104
xx Re: Detecting a key release?
« Reply #12 on: Feb 6th, 2018, 07:56am »

Thanks Rob and Richard. I guess I was assuming that the shift+uparrow inkey$ code would only be generated the first time shift was pressed and that holding the shift key down and repeatedly pressing uparrow would subsequently only generate uparrow codes. Which is why I was using the dll call to determine whether the shift key was still down.
« Last Edit: Feb 6th, 2018, 07:56am by Monkfish » User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Detecting a key release?
« Reply #13 on: Feb 6th, 2018, 08:17am »

on Feb 6th, 2018, 07:56am, Monkfish wrote:
I was assuming that the shift+uparrow inkey$ code would only be generated the first time shift was pressed

I can't speculate on how it works in LB 4, but in LBB the first byte of Inkey$ (which contains the Shift, Ctrl and Alt status) is derived by calling exactly the same API that you were using! You might as well let LBB do the work.

Richard.
User IP Logged

SarmedNafi
Junior Member
ImageImage


member is offline

Avatar




PM


Posts: 93
xx Re: Detecting a key release?
« Reply #14 on: Feb 6th, 2018, 08:48am »

Richard said,

>>> No it doesn't, Rod is right. Indeed I have looked at the code of LBB, and - as I confirmed - key release events (technically the WM_KEYUP message) are completely ignored.

I believe Richard, but it could be broken since many versions of LBB was released.

Anatoly,
We spent many nights offline, I will prepare the code.

Regards
User IP Logged

Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls