Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: leafy on February 10, 2011, 03:10:00 pm

Title: GetKey Issues
Post by: leafy on February 10, 2011, 03:10:00 pm
It's not really an issue, I just don't know how to program this:
So I have in my program

If getkey(54)
...
End

But it keeps repeating. That's not what I want. I want it to act like

getKey-->K
If K=54
...
End

But using this method you can't press multiple keys at a time.
So how do I use the first method, but tell it to stop repeating if a key is held?
Title: Re: GetKey Issues
Post by: Xeda112358 on February 10, 2011, 03:13:43 pm
getKey(54)
Ans*(Ans≠K)
If Ans
Then
Ans→K
<<More code>>
End

That is the best I can think of, sorry.
Title: Re: GetKey Issues
Post by: Ashbad on February 10, 2011, 03:18:04 pm
if it keeps repeating, it is probably due to a loop being activated before the code, or a Goto statement causing an infinite loop.  Something like this always works:

Code: [Select]
Repeat GetKey(15)
If GetKey(54)
Disp "Daddy would you like some sausage :)", i
End:End

what is probably wrong with your code is an unexpected loop earlier, I'm sure.  I would look at the code right above and below the If and the End of the getkey statement.
Title: Re: GetKey Issues
Post by: jnesselr on February 10, 2011, 03:24:13 pm
What I do, is store 0 to K, and then at the start of a Repeat loop or whatever, the first line is "If K=0:GetKey->K:End".  That way, as long as K has some value, it makes sure you read it before it checks any other keys.  And to acknowledge you read it, just write 0 to K.  This works because it won't read getkey again until you tell it to.  It has the added benefit of letting you decide if the users should be able to press the key again, or shouldn't be able to press keys.  Like storing 1 to K automatically disables key presses.  Or if you want, you can make the person continue to go right for 5 steps, but keeping a counter, and storing 0 to K when the counter is up.
Title: Re: GetKey Issues
Post by: leafy on February 10, 2011, 06:18:16 pm
Okay, I fixed it.
What I did was set K to 0, then

!If getKey(54)
1->K
End
If getKey(54) and (K=1)
0->K
...
End
Title: Re: GetKey Issues
Post by: Builderboy on February 11, 2011, 12:34:50 pm
Alternatively, you can use this piece of code and eliminate one of the getKeys()

Code: [Select]
If getKey(54)
If K
...
End
0->K
Else
1->K
End
Title: Re: GetKey Issues
Post by: ztrumpet on February 11, 2011, 05:00:29 pm
This is what I use.  It's probably horribly inefficient though:
Code: [Select]
If getKey(54)
While getKey(54)
End
<stuff here>
End
Title: Re: GetKey Issues
Post by: SirCmpwn on February 11, 2011, 06:06:35 pm
0->A
.Some sort of loop
If A
... Stuff that should only be run w/o repeat
End
...stuff with repeat
getKey=0->A
.end loop

This could be a relatively efficient solution.
Title: Re: GetKey Issues
Post by: calc84maniac on February 11, 2011, 08:13:55 pm
If you want to detect multiple keys, you could take advantage of bitwise operations.

Example:
not(K) and (getKey(1)*2+getKey(2)*2+getKey(3)*2+getKey(4)->K)->N

K holds the bitfield of raw key states, N holds the bitfield of any newly pressed keys since the last detection. You can then extract the bits using normal methods, of course. This way you can use just two variables to handle up to 16 keys (keep in mind that if you use more than 8 keys, you'll have to use the 16-bit versions of the bitwise operators)

Edit: formatting