Page 1 of 1

Keyboard repeats

Posted: Mon Sep 09, 2019 1:59 pm
by epog10
I am getting repeated responses to ExamineKeyboard/KeyboardReleased calls which i do not understand or know how to correct.

In the following skeletal code, on the next keypress the previously pressed key is returned at least a couple of times. Releasing the aPRESS = "" line still gives a similar sort of result although returning whatever is set in aPRESS.

What am I doing wrong?

Code: Select all

InitSprite()
InitKeyboard()

Enumeration
  #Window_0
EndEnumeration

Global aPRESS.s{1}                                          ;Key pressed???

Global FontID1 : FontID1 = LoadFont(1, "Arial", 16)

OpenWindow(#Window_0, 0, 0, 500, 90, "Keyboard trap testing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) ;---
OpenWindowedScreen(WindowID(#Window_0), 0, 0, 500, 300)

Repeat
  Event = WaitWindowEvent()
  
  ;aPRESS = "-"                                      ;Is this needed??
  ExamineKeyboard()                                           ;----- TEST KEYS
  If KeyboardReleased(#PB_Key_V)                              ;Red
    aPRESS = "V"
  EndIf
  
   If KeyboardReleased(#PB_Key_B)                              ;Green
     aPRESS = "B"
   EndIf
   
  If KeyboardReleased(#PB_Key_X)                              ;Exit (Protem)
    aPRESS = "X"
    Event = #PB_Event_CloseWindow 
    End
  EndIf
  Debug aPRESS
 
Until Event = #PB_Event_CloseWindow
End

Re: Keyboard repeats

Posted: Mon Sep 09, 2019 2:50 pm
by kenmo
Try this:

Code: Select all

InitSprite()
InitKeyboard()

Enumeration
  #Window_0
EndEnumeration

Global aPRESS.s{1}                                          ;Key pressed???

Global FontID1 : FontID1 = LoadFont(1, "Arial", 16)

OpenWindow(#Window_0, 0, 0, 500, 90, "Keyboard trap testing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) ;---
OpenWindowedScreen(WindowID(#Window_0), 0, 0, 500, 300)

Repeat
  
  ; Process window events as fast as possible - NOT one per ExamineKeyboard()
  Event = WindowEvent()
  While Event
    If Event = #PB_Event_CloseWindow
      Exit = #True
    EndIf
    Event = WindowEvent()
  Wend
 
  aPRESS = ""                                      ;Is this needed??
  If ExamineKeyboard()                                           ;----- TEST KEYS
    If KeyboardReleased(#PB_Key_V)                              ;Red
      aPRESS = "V"
    EndIf
   
     If KeyboardReleased(#PB_Key_B)                              ;Green
       aPRESS = "B"
     EndIf
     
    If KeyboardReleased(#PB_Key_X)                              ;Exit (Protem)
      aPRESS = "X"
      Exit= #True
    EndIf
  EndIf
  
  ; Don't print empty lines (keep debug output readable)
  If aPRESS
    Debug aPRESS
  EndIf
  
  Delay(16)
Until Exit
End
I think the main issue is you're using WaitWindowEvent() mixed with ExamineKeyboard() which makes the keyboard timing unreliable (could be very long or almost zero time between reads).

WindowEvents() tend to come in bursts of a few at once, it's better to process them all quickly in a loop before going into your keyboard checking / other program logic.

EDIT: Also, of course you need to clear out aPRESS, otherwise it holds the value and prints it again for every WindowEvent that comes in.
In your code, hit "B" then wave the mouse around the window, it prints "B" many times for every little mouse event that comes in. (Windows)

Re: Keyboard repeats

Posted: Tue Sep 10, 2019 10:48 am
by epog10
Many thanks for point out the intricacies of Window Events which I have now read about.

Furthermore your solution to my keyboard problem is just what I needed.

Much obliged - thank you.

Regards,
epog10