ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
inkey.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../../ugbc.h"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
41#if defined(__c128__) || defined(__c64__) || defined(__c64reu__)
42
43extern char DATATYPE_AS_STRING[][16];
44
45/* <usermanual>
46@keyword INKEY
47
48@english
49
50The ''INKEY' (or ''INKEY$'') capture a single character typed by the user
51without having to press enter. In other words, the program is "listening"
52to the keyboard and, as soon as a key is pressed, "captures" it and
53stores it in a variable.
54
55Unlike the ''INPUT'' command, which requires the user to press enter after
56typing, ''INKEY'' reads the character as soon as it is pressed. If no key
57has been pressed, ''INKEY$'' returns an empty string (""). The captured
58character is stored in a string variable.
59
60It is used to create interactive games where the user can control the
61character or objects by pressing specific keys, to create text menus
62where the user can select an option by pressing a letter or number
63or to create programs that respond to external events, such as
64pressing a key to start or stop an action.
65
66The ''INKEY$'' only reads one character at a time. To read a complete
67string, you must use other commands or techniques. The command
68does not usually display the pressed character on the screen.
69
70@italian
71
72''INKEY' (o ''INKEY$'') cattura un singolo carattere digitato
73dall'utente senza dover premere Invio. In altre parole, il
74programma "ascolta" la tastiera e, non appena viene premuto un
75tasto, lo "cattura" e lo memorizza in una variabile.
76
77A differenza del comando ''INPUT'', che richiede all'utente di
78premere Invio dopo aver digitato, ''INKEY'' legge il carattere
79non appena viene premuto. Se non è stato premuto alcun tasto,
80''INKEY$'' restituisce una stringa vuota (""). Il carattere
81catturato viene memorizzato in una variabile stringa.
82
83Viene utilizzato per creare giochi interattivi in ​​cui l'utente
84può controllare il personaggio o gli oggetti premendo tasti
85specifici, per creare menu di testo in cui l'utente può
86selezionare un'opzione premendo una lettera o un numero o
87per creare programmi che rispondono a eventi esterni, come
88la pressione di un tasto per avviare o interrompere un'azione.
89
90''INKEY$'' legge solo un carattere alla volta. Per leggere
91una stringa completa, è necessario utilizzare altri comandi
92o tecniche. Solitamente il comando non visualizza sullo
93schermo il carattere premuto.
94
95@syntax = INKEY$
96
97@example IF INKEY$ = "A" THEN
98@example PRINT "A has been pressed!"
99@example ENDIF
100
101@seeAlso SCANCODE
102
103</usermanual> */
104
105/* <usermanual>
106@keyword KEYGET
107
108@english
109
110The ''KEYGET' capture a single character typed by the user
111without having to press enter. In other words, the program is "listening"
112to the keyboard and, as soon as a key is pressed, "captures" it and
113stores it in a variable given as parameter.
114
115Unlike the ''INPUT'' command, which requires the user to press enter after
116typing, ''KEYGET'' reads the character as soon as it is pressed. If no key
117has been pressed, ''KEYGET'' will wait. Finally, the captured
118character is stored in a string variable.
119
120@italian
121
122''KEYGET'' cattura un singolo carattere digitato dall'utente
123senza dover premere Invio. In altre parole, il programma "ascolta"
124la tastiera e, non appena viene premuto un tasto, lo "cattura" e
125lo memorizza in una variabile specificata come parametro.
126
127A differenza del comando ''INPUT'', che richiede all'utente di premere Invio
128dopo aver digitato, ''KEYGET'' legge il carattere non appena viene premuto.
129Se non è stato premuto alcun tasto, ''KEYGET'' attenderà. Infine,
130il carattere catturato viene memorizzato in una variabile stringa.
131
132@syntax KEYGET variable
133
134@example KEYGET var$
135@example PRINT var$;" pressed"
136
137@seeAlso INKEY
138
139</usermanual> */
140
141Variable * inkey( Environment * _environment ) {
142
143 Variable * result = variable_temporary( _environment, VT_DSTRING, "(result of INKEY$)");
144 Variable * address = variable_temporary( _environment, VT_ADDRESS, "(address of temporary string)");
145 Variable * size = variable_temporary( _environment, VT_BYTE, "(size)");
146 Variable * pressed = variable_temporary( _environment, VT_BYTE, "(key pressed?)");
147 Variable * key = variable_temporary( _environment, VT_CHAR, "(key pressed)");
148
149 char resultString[MAX_TEMPORARY_STORAGE]; sprintf( resultString, " " );
150
151 variable_store_string(_environment, result->name, resultString );
152 cpu_dswrite( _environment, result->realName );
153 cpu_dsdescriptor( _environment, result->realName, address->realName, size->realName );
154
156
157 char noKeyPressedLabel[MAX_TEMPORARY_STORAGE]; sprintf(noKeyPressedLabel, "%snokeyPressed", label );
158 char finishedLabel[MAX_TEMPORARY_STORAGE]; sprintf(finishedLabel, "%sfinished", label );
159
160 cia_inkey( _environment, key->realName );
161
162 cpu_bveq( _environment, key->realName, noKeyPressedLabel );
163
164 cpu_move_8bit_indirect(_environment, key->realName, address->realName );
165 cpu_dsresize_size(_environment, result->realName, 1 );
166
167 cpu_jump( _environment, finishedLabel );
168
169 cpu_label( _environment, noKeyPressedLabel );
170
171 cpu_dsresize_size(_environment, result->realName, 0 );
172
173 cpu_label( _environment, finishedLabel );
174
175 return result;
176
177}
178
179#endif
void cpu_dsresize_size(Environment *_environment, char *_index, int _resize)
Definition 6309.c:5948
void cpu_bveq(Environment *_environment, char *_value, char *_label)
Definition 6309.c:334
void cpu_move_8bit_indirect(Environment *_environment, char *_source, char *_value)
Definition 6309.c:5239
void cpu_dswrite(Environment *_environment, char *_index)
Definition 6309.c:5927
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
void cpu_jump(Environment *_environment, char *_label)
Definition 6309.c:3739
void cpu_dsdescriptor(Environment *_environment, char *_index, char *_address, char *_size)
Definition 6309.c:5977
Variable * variable_store_string(Environment *_environment, char *_destination, char *_value)
Store a string to a variable.
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
int size
Definition _optimizer.c:678
Variable * inkey(Environment *_environment)
Definition inkey.c:43
void cia_inkey(Environment *_environment, char *_key)
Definition cia.c:115
char * name
Definition ugbc.h:979
char * realName
Definition ugbc.h:982
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_BYTE
Definition ugbc.h:450
@ VT_CHAR
Definition ugbc.h:498
@ VT_ADDRESS
Definition ugbc.h:465
@ VT_DSTRING
Definition ugbc.h:483
#define MAKE_LABEL
Definition ugbc.h:3351
char DATATYPE_AS_STRING[][16]