Codepix 2024-11-23 18:58
I’m making a program and I need to detect if the player pressed the enter key.
SP4CEBAR 2024-11-23 21:36
You can use the following program to see the ascii code of any pressed key
KEYBOARD ON
DO
KEY$ = INKEY$
TEXT 0,0, KEY$
IF KEY$ <> "" THEN NUMBER 2,0, ASC(KEY$),5
WAIT VBL
LOOP
The ascii code when pressing enter is 10 (in decimal), so you can detect enter like this:
KEY$ = INKEY$
IF ASC(KEY$) = 10 THEN PRINT "ENTER PRESSED"
SP4CEBAR 2024-11-23 21:37
ASC(KEY$) sounds a little like ASCII, doesn't it
Codepix 2024-11-23 21:53
Ok thank you!
Codepix 2024-11-23 22:01
Wait when I try to open the program it says invalid parameter, how do I fix this.
Meticulac 2024-11-23 22:39 (Edited)
After some testing, it seems the problem is that the ASC() function doesn't like handling the empty string, so you might want to use something like the following to avoid passing empty strings to ASC():
IF KEY$<>"" THEN ASCII=ASC(KEY$)
EDIT: Depending on how you're using it, you might not need to use an intermediate variable for storing the ASCII code as a number. I've put together an example program to demonstrate checking for space both with an intermediate number variable:
REM Checking for space character with intermediate variable.
KEYBOARD ON
ASCII=0
DO
IF I$<>"" THEN
PRINT I$,STR$(ASC(I$));
ASCII=ASC(I$)
ELSE
ASCII=0
END IF
IF ASCII=10 THEN PRINT "WOW!";
WAIT VBL
LOOP
And one for checking without:
REM Checking for space character without intermediate variable.
KEYBOARD ON
DO
I$=INKEY$
IF I$<>"" THEN
PRINT I$,STR$(ASC(I$));
IF ASC(I$)=10 THEN PRINT "WOW!";
END IF
WAIT VBL
LOOP