Author | 
 Topic: Detecting a key release?  (Read 357 times) | 
 
 
 | 
 
 
Monkfish
 Full Member
 
  member is offline
  
 
  
    
    
 Gender:   
Posts: 104 
 | 
  | 
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 » | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
Rod
 Full Member
 
  member is offline
  
 
  
    
    
 Gender:   
Posts: 110 
 | 
  | 
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?
 | 
 
 | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
Monkfish
 Full Member
 
  member is offline
  
 
  
    
    
 Gender:   
Posts: 104 
 | 
  | 
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.   
 | 
 
 | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
Richard Russell
 Administrator
 
  member is offline
  
 
  
    
     
  
Posts: 1348 
 | 
  | 
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.   
  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. 
 | 
 
 | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
SarmedNafi
 Junior Member
 
  member is offline
  
 
  
    
    
  
Posts: 93 
 | 
  | 
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
 | 
 
 | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
tsh73
 Full Member
 
  member is offline
  
 
  
    
    
 Gender:   
Posts: 210 
 | 
  | 
Re: Detecting a key release? 
« Reply #5 on: Feb 5th, 2018, 05:23am » | 
 | 
 
 
 
Hello SarmedNafi. Please post your code for release key event.    
 | 
 
 | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
Richard Russell
 Administrator
 
  member is offline
  
 
  
    
     
  
Posts: 1348 
 | 
  | 
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. 
 | 
 
 | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
Monkfish
 Full Member
 
  member is offline
  
 
  
    
    
 Gender:   
Posts: 104 
 | 
  | 
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
   
 | 
 
 | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
Rod
 Full Member
 
  member is offline
  
 
  
    
    
 Gender:   
Posts: 110 
 | 
  | 
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.
 | 
 
 | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
Richard Russell
 Administrator
 
  member is offline
  
 
  
    
     
  
Posts: 1348 
 | 
  | 
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. 
 | 
 
 | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
Monkfish
 Full Member
 
  member is offline
  
 
  
    
    
 Gender:   
Posts: 104 
 | 
  | 
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   
  Thanks guys   
 | 
 
 | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
Rod
 Full Member
 
  member is offline
  
 
  
    
    
 Gender:   
Posts: 110 
 | 
  | 
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 » | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
Monkfish
 Full Member
 
  member is offline
  
 
  
    
    
 Gender:   
Posts: 104 
 | 
  | 
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 » | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
Richard Russell
 Administrator
 
  member is offline
  
 
  
    
     
  
Posts: 1348 
 | 
  | 
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.
 | 
 
 | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
SarmedNafi
 Junior Member
 
  member is offline
  
 
  
    
    
  
Posts: 93 
 | 
  | 
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
 | 
 
 | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
 
 |