Reputation: 73
I am creating a game and i want that if the user hit F10 or any other function key then they the program should end.
Upvotes: 2
Views: 5870
Reputation: 1165
This code snip exits a program when function key is pressed in Qbasic:
DO
DO
' read keyboard
X$ = INKEY$
IF LEN(X$) THEN
EXIT DO
END IF
LOOP
IF LEN(X$) = 2 THEN
x = ASC(RIGHT$(X$, 1))
' exit program on F1 to F10
IF x >= 59 AND x <= 68 THEN
END
END IF
END IF
LOOP
Upvotes: -1
Reputation: 1639
For future searchers, this answer provides additional context to inform the design of mechanisms for exiting BASIC programs.
For some simplistically designed BASIC programs, the only method to exit is Control-C or Control-Break. But some emulators (such as DOSBox) do not process Control-C in a manner that will present it to the underlying program.
On some systems, you can press Ctrl-ScrollLock as a workaround:
I have a little trick for those interested: use Ctrl-ScrollLock, it behaves like Ctrl-Break with many BASIC interpreters running within DOSBox. It works with GW-BASIC, BASICA (often bundled with compatible DOSes like Compaq's), QBasic, QuickBasic, and possibly other development "workbench" interfaces.
The reason this works is a little complicated, so only read on if you're interested in knowing. DOSBox does not have true Ctrl-Break handling like real DOS, which is a combination of hardware and software interrupts and internal flags. However, the DOS Ctrl-Break handler is only a default handler that all starts with INT 9, the keyboard hardware interrupt. Many of the program development apps hook INT 9 and intercept keys before DOS sees them, so they can do their own processing. After all, the DOS default behavior for Ctrl-Break is to terminate the app, and that is often not what is wanted. The INT 9 handler code looks for the Control key being depressed by checking the shift status byte in BIOS data, and then reads scancodes from the keyboard data port 60h. The scancode for ScrollLock is 46h, and the scancode for Ctrl-Break is a 2-byte "escaped" sequence of E0h 46h, where E0h is the escape code. It seems the handler routines are often not very rigorous in their processing of the escape code, and just drop it, so Ctrl-ScrollLock ends up working the same as Ctrl-Break.
Upvotes: 2
Reputation: 49
I assume you are in the middle of some kind of BASIC program and you would like to 'get out', exit from this ?
Press ENTER
to get to a BLANK space and type system
, hit ENTER
again and you are out!
Upvotes: 1
Reputation: 81
Kinda bad that you haven't had this answered...
Well to start, you will have to find the ASCII value of whatever key you would like to have be the exit.
To do this either make a simple program to figure it out using chr$,asc(),and an input. Or just search it real quick.
What you are going to want to do is in your games area where you are using an inkey$(Which I assume you are, because most games would have movement and that is almost required) and just check for the key being pressed along with every other key. For Example:
A simple movement game:
10 cls
20 K$=inkey$
30 if K$="a" then REM move left
40 if K$="d" then REM move right
50 if K$= EXITKEY then REM EXIT
60 goto 20
-Also, sorry if this is the incorrect method, but this should work...I'm still a bit rusty on GW
Upvotes: 4