How to check keyboard state as in IsPressed(Key.Esc)

Post

Posted
Rating:
#1 (In Topic #1546)
Regular
JumpyVB is in the usergroup ‘Regular’
 My file comparison app does a lengthy processing which I wan't to able to cancel if Esc key is pressed. However I can't find a way to check for key state outside of a KeyPress() events.
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
Have a look HERE
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
JumpyVB is in the usergroup ‘Regular’

cogier said

Have a look HERE

I am looking for a way that is not tied to a Click() or KeyPress() event.

Is there a way to simply read the state of keys or a single specific key anywhere from code?
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’

JumpyVB said

cogier said

Have a look HERE

I am looking for a way that is not tied to a Click() or KeyPress() event.

Is there a way to simply read the state of keys or a single specific key anywhere from code?

No, Key.class properties (except for constants) are invalid outside of a KeyPress/KeyRelease event.

The trick would be to use Wait to allow the event loop to cycle and register key events (using Wait too much can slow the program down so maybe periodically)
something like this…

Code (gambas)

  1. Public bEscPressed As Boolean
  2.  
  3. Public Sub RunLongProcess()
  4.  
  5.   Dim dTime As Date = Now
  6.  
  7.   bEsc = False
  8.   bEscPressed = False
  9.  
  10. ' do the repetitive task...
  11.   For Each vSomething In cMyCollection
  12.  
  13.     ' do something here that takes time..
  14.  
  15.     ' let the event loop cycle every 2 seconds to catch any key events...
  16.     If DateDiff(Now, dTime, gb.second) > 2 Then
  17.       Wait
  18.       dTime = Now
  19.     Endif
  20.  
  21.   if bEsc Or If bEscPresed Then Break  ' cancel if escape pressed
  22.   Next
  23.  
  24.  
  25. Public Sub Form_KeyPress()
  26.  
  27. bEsc = Key.Code = Key["Esc"]
  28. If Not bEscPressed Then bEscPressed = bEsc
  29.  
  30.  
  31. Public Sub Form_KeyRelease()
  32.  
  33. If Key.Code = Key["Esc"] Then bEsc = False
  34.  
  35.  
  36.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
JumpyVB is in the usergroup ‘Regular’

BruceSteers said

No, Key.class properties (except for constants) are invalid outside of a KeyPress/KeyRelease event.

My main form is being unresponsive as the processing is being done. So okay, I guess I will have to resort to Button.Cancel as suggested by cogier. Only, first I need to turn my main processing loop to an asynchronous background task. Which won't be an easy ordeal.
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’

JumpyVB said

BruceSteers said

No, Key.class properties (except for constants) are invalid outside of a KeyPress/KeyRelease event.

My main form is being unresponsive as the processing is being done. So okay, I guess I will have to resort to Button.Cancel as suggested by cogier. Only, first I need to turn my main processing loop to an asynchronous background task. Which won't be an easy ordeal.

Hence the use of Wait.

No response to key events will also be no response to button clicks if the event loop cannot cycle.
Online now: No Back to the top
1 guest and 0 members have just viewed this.