title: Remap a keyboard with ioctl under linux
body: <p>I am actually trying to write a small program to catch global keyboard inputs from specific USB keyboards under linux. </p>
  
  <p>I am testing with this piece of code :</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;linux/input.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  
  static const char *const evval[3] = {
      "RELEASED",
      "PRESSED ",
      "REPEATED"
  };
  
  int main(void)
  {
      const char *dev = "/dev/input/event2";
      struct input_event ev;
      ssize_t n;
      int fd;
      char name[256]= "Unknown";
  
  //    int codes[2];
  
  //    codes[0] = 58; /* M keycap */
  //    codes[1] = 49; /* assign to N */
  
      fd = open(dev, O_RDONLY);
      if (fd == -1) {
          fprintf(stderr, "Cannot open %s: %s.\n", dev, strerror(errno));
          return EXIT_FAILURE;
      }
  
      if(ioctl(fd, EVIOCGNAME(sizeof(name)), name) &gt; 0) 
      {
          printf("The device on %s says its name is '%s'\n", dev, name);
      }
  
      /*
      int err = ioctl(fd, EVIOCSKEYCODE, codes);
      if (err) {
          perror("evdev ioctl");
      }*/
  
      while (1) {
          n = read(fd, &amp;ev, sizeof ev);
          if (n == (ssize_t)-1) {
              if (errno == EINTR)
                  continue;
              else
                  break;
          } else
          if (n != sizeof ev) {
              errno = EIO;
              break;
          }
  
          if (ev.type == EV_KEY &amp;&amp; ev.value &gt;= 0 &amp;&amp; ev.value &lt;= 2)
              printf("%s 0x%04x (%d)\n", evval[ev.value], (int)ev.code, (int)ev.code);
  
      }
  
      fflush(stdout);
      fprintf(stderr, "%s.\n", strerror(errno));
  
      return EXIT_FAILURE;
  }
  </code></pre>
  
  <p>Ths point is that I don't know how to change some input key by other. I tried by calling write() on currently red event by changing the event code, sent key was still previous one, and I tried to used ioctl with EVIOCSKEYCODE, but the call failed with an "invalid argument" error (and I'm not sure to call it correctly).</p>
  
  <p>How can I change outputed key correctly ?</p>