Author Topic: GetKey Issues  (Read 5203 times)

0 Members and 1 Guest are viewing this topic.

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
GetKey Issues
« 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?
In-progress: Graviter (...)

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: GetKey Issues
« Reply #1 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.

Ashbad

  • Guest
Re: GetKey Issues
« Reply #2 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.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: GetKey Issues
« Reply #3 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.

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Re: GetKey Issues
« Reply #4 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
In-progress: Graviter (...)

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: GetKey Issues
« Reply #5 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

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: GetKey Issues
« Reply #6 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

SirCmpwn

  • Guest
Re: GetKey Issues
« Reply #7 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.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: GetKey Issues
« Reply #8 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
« Last Edit: February 11, 2011, 08:14:30 pm by calc84maniac »
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman