Jump to content



Photo
- - - - -

Clearing The Value In Getkey?


  • Please log in to reply
12 replies to this topic

#1 Khoraski

Khoraski

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 25 April 2012 - 09:51 PM

Not sure if this is the right section for this, because I just joined, but anyways.

I'm using Casio BASIC and I'm trying to program a simple application. But I ran into a problem with GetKey that I did not have on my Texas Instrument calculator.

Here is what I was trying to do:

While 1
GetKey->K

If K="##"
...
IfEnd

If K="##"
...
IfEnd
WhileEnd

As you can see, basically it never stops looping and every time you press a key it does something. In TI-BASIC, after a key was pressed, GetKey would reset back to 0. But it does not do this in Casio BASIC. This means every time it loops, since GetKey does not reset back to 0, it will constantly execute the same IF. Such as, if I had "If K='32'" and I pressed Key #32, it would repeatedly execute the code in the IF because GetKey remains 32 and does not reset like it does in TI-BASIC.

My question is simply how can I reset GetKey and set it back to 0? I tried "0->GetKey" but it does not allow that.


Does anyone know how to do this? I just got a Casio calculator. I'm used to TI-BASIC so I'm not sure how this works completely.

#2 Forty-Two

Forty-Two

    Casio Overlord

  • Deputy
  • PipPipPipPipPipPipPip
  • 528 posts
  • Gender:Male
  • Location:Well, The sign says "You are here"...

  • Calculators:
    Casio fx-CG10 Prizm
    Casio fx-9860GII
    TI-84+ SE

Posted 25 April 2012 - 10:08 PM

While 1

GetKey->K



If K="##"

...

IfEnd



If K="##"

...

IfEnd



0->K

WhileEnd


#3 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 25 April 2012 - 10:14 PM

Hi Khoraski and welcome to UCF!

You should introduce yourself in this thread ;) : http://www.casiocalc...?showtopic=5677

Getkey tells you the last key pressed. So instead of doing:

While 1
Getkey -> K
WhileEnd

You can do this:

While 1
If Getkey = <number>
Then <stuff>
IfEnd

If Getkey = <another_number>
Then <other_stuff>
IfEnd

WhileEnd

To optimize this code even more, we can do this:

While 1
If Getkey	//if any key is pressed, the following code will execute.
Then 

If Getkey = <number>
Then <stuff>
IfEnd

If Getkey = <another_number>
Then <other_stuff>
IfEnd

WhileEnd

The If Getkey part is cool. If a key is pressed, the code in the if statement will always be executed. It does not matter if Getkey=0 or if Getkey=77, getkey will only return true when a new key is pressed.

As for setting getkey back to 0, why?

#4 Khoraski

Khoraski

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 25 April 2012 - 10:27 PM

While 1
If Getkey	//if any key is pressed, the following code will execute.
Then 

If Getkey = <number>
Then <stuff>
IfEnd

If Getkey = <another_number>
Then <other_stuff>
IfEnd

WhileEnd


I'm still getting the same problem. After I press a key, it will keep on executing the "if GetKey = <number>" over and over again.
I wanted to reset it to 0 so it would only execute the "if" statement a single time, like it did in TI-BASIC.

I tried what you said, and made it also check if GetKey returns true, but I still get the same results.

While 1
GetKey->K

If K="##"
...
IfEnd

If K="##"
...
IfEnd

0->K
WhileEnd


But GetKey does not reset, so when the loop repeats, K will get the same value again.

Edited by Khoraski, 25 April 2012 - 10:29 PM.


#5 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 25 April 2012 - 10:36 PM

While K /= 1

Getkey -> K



If K=##

...

IfEnd



WhileEnd


#6 Khoraski

Khoraski

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 25 April 2012 - 10:43 PM

While K /= 1
Getkey -> K

If K=##
...
IfEnd

WhileEnd


I'm still getting the same results. If I set it to "if K=27" (the right arrow), when I press the right arrow, it will repeatedly execute that IF statement because K's value is still 27 for every loop.

#7 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 25 April 2012 - 10:44 PM

Oh sorry here:

While K /= 1
Getkey -> K

If K=##
...
IfEnd

0 -> K

WhileEnd


#8 Khoraski

Khoraski

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 25 April 2012 - 10:52 PM

Oh sorry here:

While K /= 1
Getkey -> K

If K=##
...
IfEnd

0 -> K

WhileEnd


That works, but it still executes the code quite a bit since it takes a second to take your finger off of the button.

Is it possible to make it detect when a key is pressed and not while it's being held down?

#9 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 25 April 2012 - 10:56 PM

While K /= 1
Getkey -> K

If K=##
...
0 -> K
IfEnd
WhileEnd

That should do it ;)

#10 Forty-Two

Forty-Two

    Casio Overlord

  • Deputy
  • PipPipPipPipPipPipPip
  • 528 posts
  • Gender:Male
  • Location:Well, The sign says "You are here"...

  • Calculators:
    Casio fx-CG10 Prizm
    Casio fx-9860GII
    TI-84+ SE

Posted 25 April 2012 - 11:59 PM

That works, but it still executes the code quite a bit since it takes a second to take your finger off of the button.

Is it possible to make it detect when a key is pressed and not while it's being held down?


Not really. (Unless of course, you have some OS hooks sitting around)

#11 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 26 April 2012 - 02:42 PM

That works, but it still executes the code quite a bit since it takes a second to take your finger off of the button.

Is it possible to make it detect when a key is pressed and not while it's being held down?




If you really want to be able to do that, use C/asm ;)

#12 3298

3298

    Casio Addict

  • Members
  • PipPipPip
  • 77 posts
  • Gender:Male
  • Location:Germany

  • Calculators:
    fx-9750G Plus
    Algebra FX 2.0 (ROM 1.03,broken)
    HP 50G

Posted 26 April 2012 - 09:17 PM

What about that one:
While 1
Do
Getkey -> K
LpWhile K=0
If K=27
Then ...
IfEnd
While Getkey=K
WhileEnd
WhileEnd
I've used a similar piece of code multiple times in my programs and it always performed well. The Do-LpWhile loop waits for a key and stores it in K, the While loop at the end waits until the key is released.
EDIT: Oops, there was something wrong in the loop waiting for you to release the key (condition was the wrong way round - fixed now). I wrote that piece of code from memory and haven't touched CasioBasic in a while.

Edited by flyingfisch, 27 October 2012 - 10:35 PM.


#13 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 27 October 2012 - 10:32 PM

@3298: Oh, I haven't seen this thread in a while, that's a useful piece of code. Adding it to the truth about getkey thread ;)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users