Keypress routine wanted

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
John_Schofield
Full Member
Posts:36
Joined:Tue Mar 18, 2003 3:04 am
Location:Lancashire, UK
Contact:
Keypress routine wanted

Post by John_Schofield » Tue Mar 18, 2003 3:14 am

Hi

I have need of a simple routine to assist with a particular project where I operate a program from a free-standing numeric keypad on a laptop (not the main keyboard's switched version). I need to be able to:

a) force num lock to stay on all the time, even if the num lock key is accidentally depressed (I don't want the numeric keys to be taken out of commission by accident)

b) read and act upon the /*- and del keys being pressed without having to press enter as well (e.g. to save current data to hard disk if the * key is pressed). I haven't been able to fathom this out from the QuickBasic manual. I already have a routine which accepts a numerical string and then acts when enter is pressed but I need these special keys to act 'automatically' when pressed to perform various functions immediately but not if one is pressed part way through entering a number, if possible.

Many thanks

User avatar
Baz
Jr. Member
Posts:11
Joined:Mon Mar 10, 2003 9:55 pm
Location:Suffolk UK

Re: Keypress routine wanted

Post by Baz » Tue Mar 18, 2003 5:30 am

Hi John

Hope this may help.
The POKE statemnent solves the numlock problem
The rest is pretty straight forward:-
* key saves only after 11 or more characters have been entered otherwise it is ignored.


CLS
DEF SEG = (&H40)
text$ = ""
DO
POKE &H17, (PEEK(&H17) OR &H20)

k$ = INKEY$

IF LEN(text$) > 10 AND k$ = "*" THEN k$ = "": GOSUB savetext

IF k$ > "" AND k$ <> "*" THEN text$ = text$ + k$: LOCATE 1, 1: PRINT text$ + " "

IF k$ = CHR$(27) THEN END

LOOP

savetext:
OPEN "output.txt" FOR APPEND AS #1
text$ = text$ + CHR$(13)
PRINT #1, text$
text$ = ""
CLOSE 1
CLS
BEEP
RETURN

I liked the website. Can i have a yellow jersey  ::)

John_Schofield
Full Member
Posts:36
Joined:Tue Mar 18, 2003 3:04 am
Location:Lancashire, UK
Contact:

Re: Keypress routine wanted

Post by John_Schofield » Wed Mar 19, 2003 1:09 am

Hi Baz

Thanks for your efforts. I guess I didn't set out my stall too well. I think what I need is a set of ON KEY statements - the program records a set of numbers and times of race finishers, so that part of the routine is already set up. What I want to do is just depress the * key (for example) which then writes the stored array to disk. I just don't know the key numbers/codes for the numeric keypad and I think that's really what I need. Something like this - ON KEY(15) GOSUB SaveToDisk - although with the right key code.

This is where the routine would be used. There's a loop set up for the number of runners, which is cycled through. After each runner is times, the program waits for the next. You can eitehr just press enter, which records a time only, or type a number and press enter, which records the number against a time. * saves to disk, B or - deletes the last time recorded and goes back up one place, 9999 ends the timing sequence:

PRINT USING "####"; count%; : PRINT "("; sequence%; ")";
PRINT TAB(14); " -- ";
l% = 4: CALL String.Get(f$, l%): PLAY "MS L25 N52": REM BEEP

checknum$ = s$
IF UCASE$(checknum$) = "B" THEN
&nbsp;IF count% > 1 THEN
&nbsp; count% = count% - 1: sequence% = sequence% - 2: PRINT
&nbsp;END IF
ELSEIF checknum$ = "*" THEN
&nbsp;CALL Disksave: sequence% = sequence% - 1: PRINT
ELSEIF checknum$ = "-" THEN
&nbsp; count% = count% - 1: sequence% = sequence% - 2: PRINT
ELSE
&nbsp;IF checknum$ = "9999" THEN CALL Finished: END
&nbsp;IF checknum$ = "" THEN checknum$ = " &nbsp; &nbsp;"
&nbsp;thistime! = TIMER: runnertime! = thistime! - starttime!

this then goes on to display the number (if any) and the time and then loops around again

If you can assist with what numbers the keys on the numeric keypad have then I think a combination of your answer and this will probably do the trick!

Best wishes (and sorry - no yellow shirts!)

User avatar
frankiebaby
Global Moderator
Posts:95
Joined:Tue Apr 30, 2002 1:38 am
Location:Pennsylvania
Contact:

Re: Keypress routine wanted

Post by frankiebaby » Wed Mar 19, 2003 2:32 am

Two things to look out for:

1) Qbasic's help should tell ya the scan codes if ya read the documentation on ON KEY

2) ON KEY has one problem. It check for keypress after EVERY line of code, so if ya have a short input routine, waiting for number input say, and then you accidently press something to trigger an ON KEY event, the next step after the input line will be wherever the ON KEY tells it to go. use KEY OFF just before any times that this would be hazardous, and remember to turn it back on just afterwards.

John_Schofield
Full Member
Posts:36
Joined:Tue Mar 18, 2003 3:04 am
Location:Lancashire, UK
Contact:

Re: Keypress routine wanted

Post by John_Schofield » Wed Mar 19, 2003 2:56 am

Thanks for that. I couldn't make sense of the details in either book I've got! I've downloaded some scan zips from shareware.com to see if they help. I know what key numnber * is on the main keyboard but that isn't the same on the numeric keypad.

I don't think I'm too concerned about activating accidentally - the main purpose will be a save to disk so that's not too critical as frequent saves make shorter text files and so save more quickly (in theory anyway). The B and - options I may leave as already programmed for safety (as you point out, this may not be what was intended and accidentally delete a vaildly recorded time).

Very grateful for your response.

Guest

Could it be an extended character?

Post by Guest » Wed Mar 19, 2003 11:53 am

:-/

I think it may be an extended key code as described in the INKEY$ help:

-----------
Syntax
&nbsp;INKEY$

The INKEY$ function returns a one- or two-byte string containing a character read from the standard input device. A null string is returned if no character is waiting there. A one-character string contains the actual character read from the keyboard, while a two-character string indicates an extended code, the first character of which is hexadecimal 00..
-----------

You can try the following to see if it is extended...

-----------
CLS
DO WHILE 0 = 0
&nbsp; &nbsp;KP$ = INKEY$
&nbsp; &nbsp;IF LEFT$(KP$, 1) = CHR$(0) THEN
&nbsp; &nbsp; &nbsp; &nbsp;PRINT "2 digits:", ;
&nbsp; &nbsp; &nbsp; &nbsp;LftSide = ASC(LEFT$(KP$, 1))
&nbsp; &nbsp; &nbsp; &nbsp;RitSide = ASC(RIGHT$(KP$, 1))
&nbsp; &nbsp; &nbsp; &nbsp;PRINT LftSide; RitSide
&nbsp; &nbsp; &nbsp; &nbsp;EXIT DO
&nbsp; &nbsp;ELSE
&nbsp; &nbsp; &nbsp; &nbsp;PRINT "1 Digit - press any arrow to exit"
&nbsp; &nbsp;END IF
LOOP
END
-----------

If this doesn't help post back and I'll see if I can find the INP code in my files

--MiggyD

Post Reply