Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:30 27 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Trying to use InKey$ in a menu...

Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 11:34pm 02 Mar 2013
Copy link to clipboard 
Print this post

Hi folks.

Trying to use the code:

Do:loop until inkey$<>""

to wait for a keypress, then act on it.
This works as far as waiting till you press a key, but then the result of inkey$ is always zero.

I added the line:

cls:? val(inkey$)

so that the screen would clear, and show me the value of the keypress, but it is always zero.

Can someone help me work out what I am doing wrong?

EDIT: Hang on - inkey$ is only NOT zero for the instant when I press a key, which allows the code to move to the next line, but then it will be zero again - I will try adding a line to copy the value of inkey$ to some other variable, then check that...

EDIT: Nope. Did not help. Can anyone prod me in the right direction?Edited by Grogster 2013-03-04
Smoke makes things work. When the smoke gets out, it stops!
 
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 926
Posted: 11:47pm 02 Mar 2013
Copy link to clipboard 
Print this post

Grogster, you may have to assign a string to inkey$. ie K$= inkey$ and then test for the K$
All the best
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 11:54pm 02 Mar 2013
Copy link to clipboard 
Print this post

Good point.

Tested, still not working.

Here is my experimental code:

wfk:
key$=Ucase$(InKey$):if key$="" then goto wfk
? val(key$)


This does wait for a keypress, but the result is still zero no matter what key I press.
This is really confusing, as the fact that the loop continues once I press a key, means that InKey$(and it's copy key$) must now NOT be zero anymore, or the loop would not have looped, if you see what I mean, yet is still reports it as zero.
Smoke makes things work. When the smoke gets out, it stops!
 
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 926
Posted: 11:58pm 02 Mar 2013
Copy link to clipboard 
Print this post

My memory isn't what it used to be, but I think Geoffg covered this sometime in the past. I will search the forum and see if I can come up with his post.
 
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 926
Posted: 12:04am 03 Mar 2013
Copy link to clipboard 
Print this post

Maybe this is the post I was thinking of:
http://www.thebackshed.com/forum/forum_posts.asp?TID=4720&KW =inkey%24
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 12:40am 03 Mar 2013
Copy link to clipboard 
Print this post

  Grogster said   Good point.

Tested, still not working.

Here is my experimental code:

wfk:
key$=Ucase$(InKey$):if key$="" then goto wfk
? val(key$)


This does wait for a keypress, but the result is still zero no matter what key I press.
This is really confusing, as the fact that the loop continues once I press a key, means that InKey$(and it's copy key$) must now NOT be zero anymore, or the loop would not have looped, if you see what I mean, yet is still reports it as zero.

Grogster
You are not thinking like a programmer. inkey$ clearly has a value else the loop would not terminate. By the way, it would be much tidier if you used a do while.... loop or a do ... loop until, however that is OT.

It is your conversion of inkey$ to Ucase$ and then to Val that is invalid somewhere along the way. Here is the definition of Val
Returns the numerical value of the ‘string$’. If 'string$' is an invalid
number the function will return zero.

What about the Ucase$, is there a purpose for it. Why don't you start basic and then make it more complicated. Just equate key$ to inkey$ and print that and when you are happy then add more functionality. You will then know when you break it.
 
Juri74

Senior Member

Joined: 06/02/2012
Location: Italy
Posts: 162
Posted: 12:41am 03 Mar 2013
Copy link to clipboard 
Print this post

what version are you using?
in my maximite v4.3 this program works perfectly:
Do
A$=Inkey$
Loop Until A$<>""
Print A$

val(inkey$) return the VALUE of the ascii... if you press "3" val(a$) return 3 but if you press "f" val(a$) return 0
maybe you mean ASC(A$)
this is your original prog:
wfk:
key$=Ucase$(InKey$):if key$="" then goto wfk
? val(key$)

try this:

wfk:
key$=Ucase$(InKey$):if key$="" then goto wfk
? asc(key$)

try this:
A$="1024"
Print Val(A$)
Edited by Juri74 2013-03-04
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 12:31pm 03 Mar 2013
Copy link to clipboard 
Print this post

Thanks guys.

@BobD - Yeah, I probably was not thinking that well. It was late at night, and I did not want to go to bed till I got that working. In the end, I gave up.

I wanted to use the F1-F12 keys, so was trying to get the value of the keypress, so I could confirm what is in the manual, page 53. When I put in a if key$=145 (for F1) kind of thing, it would not loop, so I was just trying to get a confirmation of number 145 as per the table on page 53 when I press [F1].

This I was not getting.

Thanks to Juri74 for the prod I needed. Using the ASC command gives me the number I want, so I should be able to go forward now.

I have removed any reference to Ucase$ too.
Smoke makes things work. When the smoke gets out, it stops!
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 12:53pm 03 Mar 2013
Copy link to clipboard 
Print this post

Still not having any luck.

Here is the code:


Do:
key=val(inkey$) 'Get decimal value of keypress
Loop until key<>0 'Wait for any key press
if key=145 then print "[F1] pressed.":end 'Key detected
print "Wrong key."
print key
end


This waits for a key, does nothing when I DO press [F1], then returns a negative number for any of the other function keys. Pressing [F2] results in key being printed as -158.



I'm getting a bit lost - any help appreciated.

Is there a better more standard and acknowledged way of handling the function keys in MMBASIC? Perhaps there is another method I should be using?

EDIT: I have been rereading this thread, the examples, and the manual. I think I see where I am going wrong, and it is the val statement again(as BobD tried to point out). Val can only be for a number, I think. So, you can't use it to obtain a decimal value of anything other then a number.

I will make some more changes and see what happens...Edited by Grogster 2013-03-04
Smoke makes things work. When the smoke gets out, it stops!
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 01:13pm 03 Mar 2013
Copy link to clipboard 
Print this post

AHHHHHHHHHH!!!!!!!!

I think I have it worked out with everyones help.



Do:
key$=inkey$ 'Save a keypress
Loop until key$<>"" 'Wait until there is a keypress
if asc(key$)=145 then print "[F1] pressed.":end
print "Wrong key."
print asc(key$)


This is detecting the correct keypress.

I will now expand on this...

Smoke makes things work. When the smoke gets out, it stops!
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024