Using USB Keyboard

More great progress! Thanks for keeping us up to date.

petros00:
So, instead of going for a dictionary, I went for a dirty, manual method of if statements.

A dictionary would be a bit cleaner and perhaps easier to maintain, but I don't think it's necessarily faster unless it is performing a binary search behind the scenes (I don't know how it's implemented internally.) It would look something like this: (I've not tried to compile this, so it could have some silly mistakes)

Create the dictionary: (I didn't type in the whole mapping, I'll leave that to you ;D )

# Define a dictionary to map scan codes to the character to be sent to the sketch
lookup = {71:'7', 72:'8', 73:'9', <and so on...>}

Use the dictionary:

   # If this is a key press, try to look it up in the dictionary. If there is a match, send the character
    if type == 1 and value == 1:            # Check for a normal key code
        if (code in lookup):                # Is the code contained in the dictionary?
            print lookup[ code ]            # Map the key code to a character and send to the sketch

The dictionary defines a list of key/value pairs. The value to the left of the colon character is the key. If the index value being used to access the dictionary matches a key, the result of the access is the value to the right of the colon.

If you do want to still use a series of IF statements, you should consider using if-else clauses. In your existing code, let's say the code is 75: It executes the first three IF statements, and they are all false. The fourth IF evaluates true, and you have your result. But then, it still has to go through and evaluate all of the remaining IF statements - you know there's no point to it since it can't match more than one statement, but the computer doesn't necessarily know that. It would be more efficient if you re-wrote it to look something like this:

    if code == 71:
        print '7'
    elif code == 72:
        print '8'
    elif code == 73:
        print '9'
    and so on...

The advantage here is that once a match has been found, the elif (else if) is an instruction to the computer that there is no need to check any other alternatives. So, for the case of 75, it will only do four comparisons instead of all of them. For 71, it will only do one. The only time it will do all comparisons is if the code is 98.

and-guess what- again I'm gonna use ifs for every case. I might try to find out how to setup an array(I guess?) to do this instead of 18 if statements(btw, are they more taxing for the processor?).

Using a lookup table is faster than a series of IF statements, because all it needs to do is multiply the index value by the size of an array element, and then add it to the starting address of the array. But that really only works if there is a relatively small range of values and there aren't a lot of gaps in the table. With a larger range of values or with large gaps, it gets less efficient. Still, it may be more efficient than a long a string of IF statements where each comparison needs to be performed.

If you go with a series of IF statements, using the same if/else scheme as above would be more efficient. (Note that with C++ as used in the sketch, the syntax of if/else is different than it is in Python.) Be aware that if you go this way, you are setting yourself up for trouble with the way you are reading a character from the Process and then testing it in the same statement. You will be in trouble if you add more IF statement of the same type, like this:

   while (p.available()){
     if ((char)p.read() == '1'){
       Serial.println("hello");}
     if ((char)p.read() == '2'){
       Serial.println("goodbye");}

      }

This will not work as you expect. What will happen is that you will check to see if at least one character is available in the first line. Then, in the second line you will read a character and compare it to '1', and print a string if it matches. Then, in the fourth line, you will attempt to read another character from the process and compare it to 2. If a second key hasn't yet been pressed, it will wait for one. I don't really know your application, but I'm positive this is not what you want. You need to perform only one read() call, save that in a variable, and then do all of your tests against that variable:

   while (p.available()){
     char ch = (char)p.read();
     if (ch == '1'){
       Serial.println("hello");}
     else if (ch == '2'){
       Serial.println("goodbye");}

      }

BTW, The Arduino IDE loves to put the opening brace on the same line, but I think it makes it harder to read. While it's a rather personal choice, and everyone has their own style, I think this is much easier to read and the structure is more apparent, even though it takes up more lines:

   while (p.available())
   {
      char ch = (char)p.read();

      if (ch == '1')
      {
         Serial.println("hello");
      }
      else if (ch == '2')
      {
         Serial.println("goodbye");
      }
   }

An alternative between the lookup and IF statements, and what I would be inclined to use here, is a switch statement. It works well when the value being tested is a simple value (like a char) and the values being compared against are constant values. Operationally, it's much like a series of if/else statements, but it's more concise:

   while (p.available())
   {
      char ch - (char)p.read();

      switch (ch)
      {
         case '1':
            Serial.println("hello");
            break;

         case '2':
            Serial.println("goodbye");
            break;
      }
   }

Now, because the ch variable is only evaluated once, you can go back to reading and evaluating it in one step, and not need the variable. And since the statements being executed in each case are so simple, it can be reformatted quite concisely:

   while (p.available())
   {
      switch ((char)p.read())
      {
         case '1':   Serial.println("hello");      break;
         case '2':   Serial.println("goodbye");    break;
      }
   }

There are so many ways to accomplish the same thing. That's one of the things I love about programming - it gives you a chance to think about the problem, and come up with an elegant solution. There is no single correct answer. (Although there are an infinite number of wrong answers!)

Is there any way to get values from numeric keys when numlock is off? Now I don't get anything nor could I find a way.

I'm not familiar with your keypad, so I can't say. I would go back to the original script you had that printed out the full information for each key code (the type, value, etc) and see if there is a pattern you can recognize and decode. It will take a little investigative and design work.

One last question. One of the keys in the numpad is a triple 0, that is, if pressed, it will send 3 zeros one after the other. I'd like to be able to use it as well, without delaying the loop. My guess is I'll have to do it on linux side, something like, if code == 0, wait for 5ms, if within those 5 ms you didn't get more zeros, print 0, if you did, print 'h'. Is it a sound way to do it?

Are you saying that when you press this key, the keypad sends the keycode sequence for '0' three times? If so, then your plan sounds like a reasonable one. Again, I would use the old version of the script that prints out all of the keystroke information, and see if there is anything in that sequence that would make it easier to recognize that situation.