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
Keypress routine wanted
Moderators:Administrator, Global Moderator
-
- Full Member
- Posts:36
- Joined:Tue Mar 18, 2003 3:04 am
- Location:Lancashire, UK
- Contact:
Re: Keypress routine wanted
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 ::)
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 ::)
-
- Full Member
- Posts:36
- Joined:Tue Mar 18, 2003 3:04 am
- Location:Lancashire, UK
- Contact:
Re: Keypress routine wanted
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
IF count% > 1 THEN
count% = count% - 1: sequence% = sequence% - 2: PRINT
END IF
ELSEIF checknum$ = "*" THEN
CALL Disksave: sequence% = sequence% - 1: PRINT
ELSEIF checknum$ = "-" THEN
count% = count% - 1: sequence% = sequence% - 2: PRINT
ELSE
IF checknum$ = "9999" THEN CALL Finished: END
IF checknum$ = "" THEN checknum$ = " "
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!)
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
IF count% > 1 THEN
count% = count% - 1: sequence% = sequence% - 2: PRINT
END IF
ELSEIF checknum$ = "*" THEN
CALL Disksave: sequence% = sequence% - 1: PRINT
ELSEIF checknum$ = "-" THEN
count% = count% - 1: sequence% = sequence% - 2: PRINT
ELSE
IF checknum$ = "9999" THEN CALL Finished: END
IF checknum$ = "" THEN checknum$ = " "
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!)
- frankiebaby
- Global Moderator
- Posts:95
- Joined:Tue Apr 30, 2002 1:38 am
- Location:Pennsylvania
- Contact:
Re: Keypress routine wanted
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.
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.
-
- Full Member
- Posts:36
- Joined:Tue Mar 18, 2003 3:04 am
- Location:Lancashire, UK
- Contact:
Re: Keypress routine wanted
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.
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.
Could it be an extended character?
:-/
I think it may be an extended key code as described in the INKEY$ help:
-----------
Syntax
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
KP$ = INKEY$
IF LEFT$(KP$, 1) = CHR$(0) THEN
PRINT "2 digits:", ;
LftSide = ASC(LEFT$(KP$, 1))
RitSide = ASC(RIGHT$(KP$, 1))
PRINT LftSide; RitSide
EXIT DO
ELSE
PRINT "1 Digit - press any arrow to exit"
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
I think it may be an extended key code as described in the INKEY$ help:
-----------
Syntax
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
KP$ = INKEY$
IF LEFT$(KP$, 1) = CHR$(0) THEN
PRINT "2 digits:", ;
LftSide = ASC(LEFT$(KP$, 1))
RitSide = ASC(RIGHT$(KP$, 1))
PRINT LftSide; RitSide
EXIT DO
ELSE
PRINT "1 Digit - press any arrow to exit"
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