2

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?

3
  • 1
    Is the buf string used in keyboardKey() function or outside that function? If it is used inside, you can define a static string, and a static int index which initially set to 0. Each time you read a key, you copy that to ith place in the string and move i by 1. If you are useing buf outside the keyboardKey() function, you may need to define the string outside the function, and pass the string as a parament to the function. Commented Oct 31, 2015 at 16:49
  • 1
    You have char buf[4]; and are using it as a string. (e.g. printf("%c\n",buf[0]);) that means you have at most 3 characters you can store in buf (reserving the last, buf[3] for the null-terminating character 0). You can use static int i; causing i to retain it's last value on each function call, enabling you to use buf as a ring-buffer to that extent (e.g. at the end, i++; if (i == 3) i = 0;). Or, you can change the return char keyboardKey (int key) and store each key as it is returned. There are many ways to approach this. Commented Oct 31, 2015 at 16:59
  • thanks ! that helped a lot ! Commented Nov 6, 2015 at 16:17

1 Answer 1

1

Not quite sure what you're trying to accomplish. Anyway, if you want to store your buffer's state in each call of keyboardKey, you could declare it as static like below:

#define BUF_SIZE 4

void keyboardKey(int key) {
  static char buf[BUF_SIZE];
  static int  i = 0;

  if(i > ((int)sizeof(buf) - 2)) {
    i = 0;
    memset(buf, 0, sizeof(buf));
  }

  printf("you typed %c\n",key);
  buf[i] = key; 

  genieWriteStr (1, buf);

  buf[i++] = key;
  printf("%s\n",buf);
}

LIVE DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

That works, thank you so much! but can you explain or maybe post a website to a tutorial what does this line mean (int)sizeof(buf) - 2)? Why -2?
@PaulBernhardWagner You have a buffer of N elements (i.e., N = sizeof(buf)), -1 element for the \0 terminating character in order for your buffer to be interpreted as a valid string, you have left with N - 1 usable elements. Now C counts from 0, thus the index i must take values from the sequence {0, 1, 2, ..., N - 2}. Consequently, when the index reaches a value that is greater than N - 2, the buffer along with the associated index must be reset. That's why the -2. Hope that cleared things out for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.