Basic

Moderators: None (Apply to moderate this forum)
Number of threads: 1677
Number of posts: 4766

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Inkey$ question... Posted by Malver on 11 Jul 2003 at 2:17 PM
Is there anyway to use Inkey$ to make smooth keyboard input (as well as multiple keys down at once)? If not, is there another way to achieve this in Qbasic?

Thanks in advance,

- Malver
Report
Re: Inkey$ question... Posted by WaltP on 11 Jul 2003 at 4:34 PM
: Is there anyway to use Inkey$ to make smooth keyboard input (as well as multiple keys down at once)? If not, is there another way to achieve this in Qbasic?
:
: Thanks in advance,
:
: - Malver
:
Please define your request better. Smooth keyboard input??
What do you want to do with mutiple keys at once? What type of keys?

Examples and code will help explain what you want.

----------------
Walt


Report
Re: Inkey$ question... Posted by Malver on 11 Jul 2003 at 6:39 PM
: Please define your request better. Smooth keyboard input??
: What do you want to do with mutiple keys at once? What type of keys?

Keyboard keys? I need to be able to detect if a key is down or not. Is that clearer?

: Examples and code will help explain what you want.

I can't provide an example if I don't know how to do it!
Report
Re: Inkey$ question... Posted by WaltP on 12 Jul 2003 at 9:51 PM
: : Please define your request better. Smooth keyboard input??
: : What do you want to do with mutiple keys at once? What type of keys?
:
: Keyboard keys? I need to be able to detect if a key is down or not. Is that clearer?

Not really. Your request can be interpreted in many ways:

easy: I need to know if a key has been pressed and what key it was -- standard keyboard function

hard: I need to know that a key is physically pressed and when it is released -- keyboard manipulation like used in a game. While the 'L' key is pressed, move something to the left. When released, stop.

impossible: I need to know that the 'U' and 'K' keys are pressed at the same time to set the detinator and blow up the computer.

I've learned not to make assumptions because many people post very strange requests.


----------------
Walt


Report
Re: Inkey$ question... Posted by Malver on 13 Jul 2003 at 8:44 AM
: hard: I need to know that a key is physically pressed and when it is released -- keyboard manipulation like used in a game. While the 'L' key is pressed, move something to the left. When released, stop.

This would be what I'm after. Sorry for the lack of clarity.
Report
Re: Inkey$ question... Posted by tlmcduffie on 24 Jul 2003 at 7:15 PM
: : hard: I need to know that a key is physically pressed and when it is released -- keyboard manipulation like used in a game. While the 'L' key is pressed, move something to the left. When released, stop.
:
: This would be what I'm after. Sorry for the lack of clarity.
:
The INKEY$ function polls the keyboard for a single keystroke and it happens in just an instant, then your program continues on to the next sequential statement (immediately following the INKEY$). It appears you want to develop an event-driven program, and this can be done using INKEY$. Here is an example of incrementing and decrementing OVER and DOWN coordinates for an 80 X 25 grid, using the keyboard keys of U, D, L and R. The ESC key (CHR$(27) will terminate the program.

This program will move an asterisk around anywhere on the screen. Please note the importance of tracking the previous coordinates for erasing the old asterisk prior to displaying the newly-located asterisk as a key is pressed. pover% means PreviousOverand pdown% means PreviousDown. Also note the editing that takes place on the coordinates to ensure you don't attempt to move out of the 80 X 25 spectrum.

Also note the comments beginning with the ' character. Hope these help.

I have also tested this program, so you should be able to just copy and paste it into a .BAS text file and compile, link and run it.

** NOTE **
Following this program listing are some instructions on how you can use the actual arrows on the keyboard to move the asterisk around. Dont miss this !!!!!!!!!!!!!!!!!!!!

5 over%=1:down%=1:pover%=1:pdown%=1 'start in upper left-hand corner
8 color 7,0:cls
9 locate 1,1:print "*";
10 k$=inkey$
15 if k$=chr$(27) then end 'end program if esc key pressed
20 if K$="" then 10 'if no key was down when inkey occurred, loop back
22 'to 10 for another key.
30 if k$="L" or k$="l" then over%=over%-1 'moving to left
40 if k$="R" or k$="r" then over%=over%+1 'moving to right
50 if k$="U" or k$="u" then down%=down%-1 'moving up
60 if k$="D" or k$="d" then down%=down%+1 'moving down
70 'make sure up and down are within 80 X 25 spectrum
80 if over%<1 then over%=1
90 if over%>80 then over%=80
100 if down%<1 then down%=1
110 if down%>25 then down%=25
120 'see if coordinates changed. if they didn't, either wrong key was
130 'pressed or coordinates attempted to go off the screen.
140 if (over%=pover%) and (down%=pdown%) then 10
150 'erase old asterisk, display new asterisk
160 locate pdown%,pover%
170 print " ";
180 locate down%,over%
190 print "*";
195 '!!! VERY IMPORTANT !!! Save the previous coordinates !!!
198 pdown%=down%:pover%=over%
200 goto 10

!!!!!!! Here is how you can use the arrows !!!!!!!!!!!

Whenever certain keys are pressed, the inkey$ function returns a two-byte string instead of only one. Some of these keys include the up, down, left and right arrows. Whenever one of these keys is pressed, the second byte is checked. Example:

10 k$=inkey$
15 if k$="" then 10 'if no key is pressed, poll keyboard again.
20 if len(k$)=2 then 30
25 end 'any key pressed other than arrows will end program
30 kr$=right$(k$,1)
40 if kr$="H" then print "UP ARROW"
50 if kr$="P" then print "DOWN ARROW"
60 if kr$="K" then print "LEFT ARROW"
70 if kr$="M" then print "RIGHT ARROW"
80 goto 10


Make the following changes to the ASTERISK moving program to utilize the arrows:

Insert this line

23 kr$=right$(k$,1)

Change lines 30, 40, 50 and 60 to look as follows:

30 if kr$="K" then over%=over%-1 'moving to left
40 if kr$="M" then over%=over%+1 'moving to right
50 if kr$="H" then down%=down%-1 'moving up
60 if kr$="P" then down%=down%+1 'moving down

Save and recompile your program, then use the arrows instead of L,R,U D.

Hope this helps!! Good Luck!!

Report
? on above program Posted by mercman2000 on 24 Jul 2003 at 7:44 PM
On your arrow keys example, I noticed you just did HPMK for detection. The first example on arrow keys I saw utilized CHR$(0) + "H" for the up arrow key. You said there's two bytes that are sent back. Is the CHR$(0) the first one? I'm assuming doing it this way will let the program interpret the keypress as the actual up arrow, and it won't get it confused with the user pressing the actual letter H (or h, as the case may be). I could be wrong, but that's my take on it. You probably want to modify the program as such so there isn't confusion, and you have those keys left open for usage.

: 10 k$=inkey$
: 15 if k$="" then 10 'if no key is pressed, poll keyboard again.
: 20 if len(k$)=2 then 30
: 25 end 'any key pressed other than arrows will end program
: 30 kr$=right$(k$,1)
: 40 if kr$="H" then print "UP ARROW"
: 50 if kr$="P" then print "DOWN ARROW"
: 60 if kr$="K" then print "LEFT ARROW"
: 70 if kr$="M" then print "RIGHT ARROW"
: 80 goto 10

An alternate structure you can use (no line numbers) is the following:

DO
kbd$ = INKEY$
IF kbd$ = CHR$(0) + "H" THEN PRINT "Up Arrow"
IF kbd$ = CHR$(0) + "P" THEN PRINT "Down Arrow"
IF kbd$ = CHR$(0) + "K" THEN PRINT "Left Arrow"
IF kbd$ = CHR$(0) + "M" THEN PRINT "Right Arrow"
IF kbd$ = CHR$(27) THEN EXIT DO
LOOP
END

The CHR$(27) is the ESC key on your keyboard. Press it to exit from this code.
Report
Re: ? on above program - answer Posted by tlmcduffie on 25 Jul 2003 at 7:21 AM
I have to admit the DO structure along with checking chr$(0) probably looks cleaner and is fewer lines of code than the example I supplied. It is just a matter of preference as to the style the programmer chooses.

To answer your question on how the program differentiates the arrows from other keystrokes, note how the first program checks for the length: len(k$)=2. If the length is NOT 2, then an ordinary key, other than an arrow was pressed. Also note that the first program does not evaluate any keystroke until a non-null character is detected, at which time only the second byte is checked. The DO structure, however, is repeatedly performing five IF's, each checking 2 bytes and additionally invoking the chr$ function 5 times on every pass through the loop. Considering this I must argue the latter may not be quite as fast or efficient.

Thanks for the feedback!!

Report
Re: Inkey$ question... Posted by Malver on 25 Jul 2003 at 11:31 AM
: The INKEY$ function polls the keyboard for a single keystroke and it happens in just an instant, then your program continues on to the next sequential statement (immediately following the INKEY$). It appears you want to develop an event-driven program, and this can be done using INKEY$. Here is an example of incrementing and decrementing OVER and DOWN coordinates for an 80 X 25 grid, using the keyboard keys of U, D, L and R. The ESC key (CHR$(27) will terminate the program.
:
: This program will move an asterisk around anywhere on the screen. Please note the importance of tracking the previous coordinates for erasing the old asterisk prior to displaying the newly-located asterisk as a key is pressed. pover% means PreviousOverand pdown% means PreviousDown. Also note the editing that takes place on the coordinates to ensure you don't attempt to move out of the 80 X 25 spectrum.
[snip code]
:
: Hope this helps!! Good Luck!!

I tested out the code that you provided, and it does not satisfy what I specified that I needed.

Take your asterisk-moving program for example. If you hold down the right arrow key, you will move to the right once, and then move continuously to the right after a moment. I want to be able to have it move continuously as soon as the key is pressed, as opposed to what you have posted here.
Report
Re: Inkey$ question... (code sample) Posted by mercman2000 on 25 Jul 2003 at 1:52 PM
The following code is really sensitive to keyboard presses.

This responds to the right arrow key only

CLS
DO
LOCATE 1, 1
IF INP(&H60) = 77 THEN x = x + 1: PRINT x
LOOP

A quick keypress on this system yields a loop of 1800+ times, demonstrated by x.

Or, is this what you are after:

CLS
LOCATE 1, 1
PRINT "*"
row = 1
column = 1
action$ = "null"
DO
kbd$ = INKEY$
IF kbd$ = CHR$(0) + "M" THEN action$ = "right"
IF kbd$ = CHR$(27) THEN action$ = "null"
SELECT CASE action$
CASE "null"
CASE "right"
'LOCATE [row%] [,[column%]
LOCATE row, column: PRINT " "
column = column + 1
IF column > 80 THEN column = 1
LOCATE row, column: PRINT "*"
FOR z = 0 TO 25000: NEXT z
END SELECT
LOOP

Press the right arrow key to start things, press esc to stop it at any time.
Report
Re: Inkey$ question... (code sample) Posted by Malver on 25 Jul 2003 at 7:13 PM
This message was edited by Malver at 2003-7-25 19:17:38

: The following code is really sensitive to keyboard presses.
:
: This responds to the right arrow key only
:
: CLS
: DO
: LOCATE 1, 1
: IF INP(&H60) = 77 THEN x = x + 1: PRINT x
: LOOP
:
: A quick keypress on this system yields a loop of 1800+ times, demonstrated by x.

This is exactly what I am after. My only problem is that the system 'locks' up after pressing the key for a while (as in, it beeps at me). How can I prevent it from doing this?

Also, is there a way to detect multiple keyboard presses at once?
Report
Re: Inkey$ question... (code sample) Posted by mercman2000 on 26 Jul 2003 at 11:52 PM
: This message was edited by Malver at 2003-7-25 19:17:38

: : The following code is really sensitive to keyboard presses.
: :
: : This responds to the right arrow key only
: :
: : CLS
: : DO
: : LOCATE 1, 1
: : IF INP(&H60) = 77 THEN x = x + 1: PRINT x
: : LOOP
: :
: : A quick keypress on this system yields a loop of 1800+ times, demonstrated by x.
:
: This is exactly what I am after. My only problem is that the system 'locks' up after pressing the key for a while (as in, it beeps at me). How can I prevent it from doing this?
:
: Also, is there a way to detect multiple keyboard presses at once?
:

CLS
DO
LOCATE 1, 1
IF INP(&H60) = 77 THEN x = x + 1: PRINT x
something$ = inkey$
LOOP

The above will stop the beeping you are describing. As far as reading 1 or more keypresses at the same time, I can't help you with that.
Report
Re: Inkey$ question... (code sample) Posted by WaltP on 27 Jul 2003 at 11:40 PM
:
: Also, is there a way to detect multiple keyboard presses at once?
:

No, the last key pressed it the only key pressed as far as the system is concerned.


----------------
Walt


Report
Re: Inkey$ question... (multiple keypresses) Posted by mercman2000 on 28 Jul 2003 at 8:05 AM
: No, the last key pressed it the only key pressed as far as the system is concerned.

You might be able to with some Assembly code, but that's probably beyond the scope of this board. I know I certainly don't know how to even begin to approach it. But in Basic, even with the inp code, I don't think it's possible. In any case, good luck.
Report
Better Question Please! Posted by TBushmaker on 12 Jul 2003 at 5:17 PM
INKEY$ means INput a single KEYstroke from the keyboard, no "return" or "enter" is necessary to terminate this function.

?Smooth Keyboard Input? What does this mean?

?Multiple keys down? Such as <Ctrl> + <A> to do something?

: Is there anyway to use Inkey$ to make smooth keyboard input (as well as multiple keys down at once)? If not, is there another way to achieve this in Qbasic?
:
: Thanks in advance,
:
: - Malver
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.