I connected a display with a keyboard to a Raspberry Pi and when I press a key, it displays the key on the screen. I am using this library and parts of this program.
So I understood that this code passes the key to my keyboardKey function.
if (reply->object == GENIE_OBJ_KEYBOARD)
{
if (reply->index == 0) // Only one keyboard
keyboardKey (reply->data) ;
The keyboardKey function then displays the key on the screen and saves the value into the buf array.
void keyboardKey (int key)
{
char buf[4] ;
int i ;
printf("you typed %c\n",key) ; //shows the typed key in the terminal
sprintf(buf, "%c",key); //transforms the key into a string which is required for using genieWrite
genieWriteStr (1, buf) ; //writes the string to the screen. 1 is the index of the text box
buf[i] = key ; //stores the key in the array
printf("%c\n",buf[0]); //prints the array [0] in the terminal
}
The problem is it only shows one input. Everytime when a key is pressed it gets overwritten and the textbox only shows the last input. But what I want is for example to display a name.
So therefore I have to store each key that is passed to the function into the array without overwriting any of the previous ones.
I know how to store and print user input from the terminal with scanf or getchar and the use for loops to store and print the output, however here I am stuck. I was thinking of using i++ at the end but then what value should i get in the first place?
Anyone an idea or a term I could google for, I'm pretty sure this is a common problem?
char buf[4];and are using it as a string. (e.g.printf("%c\n",buf[0]);) that means you have at most3characters you can store inbuf(reserving the last,buf[3]for the null-terminating character0). You can usestatic int i;causingito retain it's last value on each function call, enabling you to usebufas a ring-buffer to that extent (e.g. at the end,i++; if (i == 3) i = 0;). Or, you can change the returnchar keyboardKey (int key)and store each key as it is returned. There are many ways to approach this.