Posts: 456
Threads: 63
Joined: Apr 2022
Reputation:
10
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
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.)
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: 1,507
Threads: 160
Joined: Apr 2022
Reputation:
116
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: 456
Threads: 63
Joined: Apr 2022
Reputation:
10
(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.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.)
|