Posts: 652
Threads: 96
Joined: Apr 2022
Reputation:
22
Is the following sample using the _KeyClear, _Limit, and _keyhit functions correctly? (not worried about the rest at this point). It seems to be ok, but I'm not sure about placement of _limit, and whether it will be called more times than necessary.
_KeyClear
Print "hit a key"
GetAnAction:
_Limit 30 ' limit resource usage
k = _KeyHit ' get code of key press
Locate 12, 40: Print k; Space$(5) ' erase previous key code
If k < 1 Then GoTo GetAnAction ' if there are no keys pressed, have another look - but only do this max 30 times per second
Locate 13, 1: Print Space$(13); ' erase previous key code announcement
Locate 13, 1
Print "ok, saw"; k ' announce key that was recognized
GoTo GetAnAction ' look for another key press
Posts: 14
Threads: 2
Joined: Oct 2022
Reputation:
1
Phil
Add this line, Locate 10, 10: Print n:n = n + 1,
Then run it
Then move the _Limit 30 to be before the GetAnAction: label and run it again to see the difference.
_KeyClear
Print "hit a key"
GetAnAction:
_Limit 30 ' limit resource usage
k = _KeyHit ' get code of key press
Locate 12, 40: Print k; Space$(15) ' erase previous key code
'###
Locate 10, 10: Print n:n = n + 1
'####
If k < 1 Then GoTo GetAnAction ' if there are no keys pressed, have another look - but only do this max 30 times per second
Locate 13, 1: Print Space$(20); ' erase previous key code announcement
Locate 13, 1
Print "ok, saw"; k ' announce key that was recognized
GoTo GetAnAction ' look for another key press
Posts: 2,698
Threads: 327
Joined: Apr 2022
Reputation:
217
Limit placement looks fine to me -- it's in your innermost loop, and the one where the user is going to interact with the most.
From what I see, after I restructured the code to get rid of the GOTOs, you might want to move the _KEYCLEAR however.
Code: (Select All) Print "hit a key"
Do
_KeyClear ' clear the buffer before looking for any new keystrokes
Do
_Limit 30 ' limit resource usage
k = _KeyHit ' get code of key press
Locate 12, 40: Print k; Space$(5) ' erase previous key code
Loop Until k > 0 ' if there are no keys pressed, have another look - but only do this max 30 times per second
Locate 13, 1: Print Space$(13); ' erase previous key code announcement
Locate 13, 1: Print "ok, saw"; k ' announce key that was recognized
Loop Until k = 27 ' look for another key press (exit with ESC)
For this short of an example, I'd think the above is what you want. Just ask yourself, "Do I want to process current key hits, or hits off the keyboard buffer?" Here, it appears you're just wanting to print the code for current key hits -- thus the _KEYCLEAR before the _KEYHIT loop. If you're wanting to check the whole buffer and process it however, you'd probably want that _KEYCLEAR outside the main loop completely, and between the PRINT and the DO statement in the code above.
Posts: 652
Threads: 96
Joined: Apr 2022
Reputation:
22
(11-23-2022, 02:42 AM)SMcNeill Wrote: Limit placement looks fine to me -- it's in your innermost loop, and the one where the user is going to interact with the most.
From what I see, after I restructured the code to get rid of the GOTOs, you might want to move the _KEYCLEAR however.
Code: (Select All) Print "hit a key"
Do
_KeyClear ' clear the buffer before looking for any new keystrokes
Do
_Limit 30 ' limit resource usage
k = _KeyHit ' get code of key press
Locate 12, 40: Print k; Space$(5) ' erase previous key code
Loop Until k > 0 ' if there are no keys pressed, have another look - but only do this max 30 times per second
Locate 13, 1: Print Space$(13); ' erase previous key code announcement
Locate 13, 1: Print "ok, saw"; k ' announce key that was recognized
Loop Until k = 27 ' look for another key press (exit with ESC)
For this short of an example, I'd think the above is what you want. Just ask yourself, "Do I want to process current key hits, or hits off the keyboard buffer?" Here, it appears you're just wanting to print the code for current key hits -- thus the _KEYCLEAR before the _KEYHIT loop. If you're wanting to check the whole buffer and process it however, you'd probably want that _KEYCLEAR outside the main loop completely, and between the PRINT and the DO statement in the code above.
Thanks Steve. I think I get it, I'll go play some more.
|