title: How to generate key strokes events with the Input Subsystem
body: <p>I am writing a Key board emulator program in Linux, As a start I was able to render key strokes into X11 window but this is not working in virtual terminals and try out a different way. I referred to <a href="http://thiemonge.org/getting-started-with-uinput" rel="nofollow noreferrer">http://thiemonge.org/getting-started-with-uinput</a> and tried with uinput kernel module. According to the tutorial key strokes can be injected as a uinput event and I wrote below code accordingly.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.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;linux/uinput.h&gt;
  #include &lt;iostream&gt;
  
  #include &lt;time.h&gt;
  #include &lt;string&gt;
  #define die(str, args...) do { \
  perror(str); \
  exit(EXIT_FAILURE); \
  } while(0)
  
  int main(void)
  {
      int                    fd_keyEmulator;
      struct uinput_user_dev uidev;
      struct input_event     ev;
      int                    dx, dy;
      int                    i;
  
      fd_keyEmulator = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
      if(fd_keyEmulator &lt; 0)
      {
          std::cout &lt;&lt; "error: open : " &lt;&lt;  strerror(errno) &lt;&lt; std::endl;
      }
  
      int ret;
      //ret = ioctl(fd_keyEmulator, UI_SET_EVBIT, EV_KEY);
      //ret = ioctl(fd_keyEmulator, UI_SET_KEYBIT, KEY_D);
      //ret = ioctl(fd_keyEmulator, UI_SET_EVBIT, EV_SYN);
      sleep(5);
      if (ioctl(fd_keyEmulator, UI_SET_EVBIT, EV_KEY) &lt; 0)
      {
          std::cout &lt;&lt; "test 1 ..." &lt;&lt; std::endl;
          die("error: ioctl");
      }
      if (ioctl(fd_keyEmulator, UI_SET_KEYBIT, KEY_D) &lt; 0)
      {
          std::cout &lt;&lt; "test 2 ..." &lt;&lt; std::endl;
          die("error: ioctl");
      }
      if (ioctl(fd_keyEmulator, UI_SET_EVBIT, EV_REL) &lt; 0)
      {
          std::cout &lt;&lt; "test 3 ..." &lt;&lt; std::endl;
          die("error: ioctl");
      }
  
  
      memset(&amp;uidev, 0, sizeof(uidev));
      snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "keyboard-emulator");
      uidev.id.bustype = BUS_USB;
      uidev.id.vendor  = 0x1;
      uidev.id.product = 0x1;
      uidev.id.version = 1;
  
      std::cout &lt;&lt; "Writing key press..." &lt;&lt; std::endl;
      if(write(fd_keyEmulator, &amp;uidev, sizeof(uidev)) &lt; 0)
          std::cout &lt;&lt; "error: write" &lt;&lt;  strerror(errno) &lt;&lt; std::endl;
  
      if(ioctl(fd_keyEmulator, UI_DEV_CREATE) &lt; 0)
          std::cout &lt;&lt; "error: ioctl" &lt;&lt;  strerror(errno) &lt;&lt; std::endl;
  
  
      memset(&amp;ev, 0, sizeof(ev));
      ev.type = EV_REL;
      ev.code = KEY_D;
      ev.value = 1;
  
      //ret = write(fd_keyEmulator, &amp;ev, sizeof(ev));
      if (write(fd_keyEmulator, &amp;ev, sizeof (struct input_event)) &lt; 0)
                  die("error: write");
      if (write(fd_keyEmulator, &amp;ev, sizeof (struct input_event)) &lt; 0)
                  die("error: write");
      if (write(fd_keyEmulator, &amp;ev, sizeof (struct input_event)) &lt; 0)
                  die("error: write");
      if (write(fd_keyEmulator, &amp;ev, sizeof (struct input_event)) &lt; 0)
                  die("error: write");
  
  
      if(ioctl(fd_keyEmulator, UI_DEV_DESTROY) &lt; 0)
          std::cout &lt;&lt; "error: ioctl" &lt;&lt;  strerror(errno) &lt;&lt; std::endl;
  
      close(fd_keyEmulator);
  
  }
  </code></pre>
  
  <p>in this case, what I am trying is to generate a uinput events for key stroke 'd'. but with the program execution I cannot see anything. 
  can somebody help me to verify this program. It is not clear how to inject a key stroke with uinput subsystem in the tutorial as well.</p>
  
  <p>EDIT:
  I wrote a different program, but I cannot see any output. I got lost and any help appreciate.</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.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;linux/uinput.h&gt;
  #include &lt;iostream&gt;
  
  #include &lt;time.h&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  /*
   * 
   */
  int main(int argc, char** argv) {
  
      // create uinput file descriptor
      int fd_key_emulator;                                                                    
  
      // open file descriptor
      fd_key_emulator = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
      if(fd_key_emulator &lt; 0)
      {
          std::cout &lt;&lt; "error in open : " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
      }
  
      // uinput_user_dev struct for fake keyboard
      struct uinput_user_dev dev_fake_keyboard;
      memset(&amp;dev_fake_keyboard, 0, sizeof(uinput_user_dev));
      snprintf(dev_fake_keyboard.name, UINPUT_MAX_NAME_SIZE, "kb-emulator");
      dev_fake_keyboard.id.bustype = BUS_USB;
      dev_fake_keyboard.id.vendor = 0x01;
      dev_fake_keyboard.id.product = 0x01;
      dev_fake_keyboard.id.version = 1;
  
  
  
      /**configure the input device to send type of events, inform to subsystem which 
       * type of input events we are using via ioctl calls. 
       * UI_SET_EVBIT ioctl request is used to applied on uinput descriptor to enable a type of event.
       **/
      // enable key press/release event
      if(ioctl(fd_key_emulator, UI_SET_EVBIT, EV_KEY))
      {
          std::cout &lt;&lt; "Error in ioctl : UI_SET_EVBIT : EV_KEY : " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
      }
  
      // enable set of KEY events here
      if(ioctl(fd_key_emulator, UI_SET_KEYBIT, KEY_A))
      {
          std::cout &lt;&lt; "Error in ioctl : UI_SET_KEYBIT : KEY_A : " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
      }
  
      // enable synchronization event
      if(ioctl(fd_key_emulator, UI_SET_EVBIT, EV_SYN))
      {
          std::cout &lt;&lt; "Error in ioctl : UI_SET_EVBIT : EV_SYN : " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
      }
  
      // now write the uinput_user_dev structure into uinput file descriptor
      if(write(fd_key_emulator, &amp;dev_fake_keyboard, sizeof(uinput_user_dev)) &lt; 0)
      {
          std::cout &lt;&lt; "Error in write(): uinput_user_dev struct into uinput file descriptor: " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
      }
  
      // create the device via an IOCTL call 
      if(ioctl(fd_key_emulator, UI_DEV_CREATE))
      {
          std::cout &lt;&lt; "Error in ioctl : UI_DEV_CREATE : " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
      }
      // now fd_key_emulator represents the end-point file descriptor of the new input device. 
  
  
      // struct member for input events
      struct input_event key_input_event;
      memset(&amp;key_input_event, 0, sizeof(input_event));
  
      // key press event for 'a'
      key_input_event.type = EV_KEY;
      key_input_event.code = KEY_A;
      key_input_event.value = 1;
  
      // now write to the file descriptor
      if(write(fd_key_emulator, &amp;key_input_event, sizeof(input_event)) &lt; 0)
      {
          std::cout &lt;&lt; "Error write : KEY_A press : " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
      }
  
      memset(&amp;key_input_event, 0, sizeof(input_event));
      // EV_SYN for key press event
      key_input_event.type = EV_SYN;
      key_input_event.code = SYN_REPORT;
      key_input_event.value = 0;
  
      // now write to the file descriptor
      if(write(fd_key_emulator, &amp;key_input_event, sizeof(input_event)) &lt; 0)
      {
          std::cout &lt;&lt; "Error write : EV_SYN for key press : " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
      }
  
      memset(&amp;key_input_event, 0, sizeof(input_event));
  
      // key release event for 'a'
      key_input_event.type = EV_KEY;
      key_input_event.code = KEY_A;
      key_input_event.value = 0;
  
      // now write to the file descriptor
      if(write(fd_key_emulator, &amp;key_input_event, sizeof(input_event)) &lt; 0)
      {
          std::cout &lt;&lt; "Error write : KEY_A release : " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
      }
  
      memset(&amp;key_input_event, 0, sizeof(input_event));
      // EV_SYN for key press event
      key_input_event.type = EV_SYN;
      key_input_event.code = SYN_REPORT;
      key_input_event.value = 0;
  
      // now write to the file descriptor
      if(write(fd_key_emulator, &amp;key_input_event, sizeof(input_event)) &lt; 0)
      {
          std::cout &lt;&lt; "Error write : EV_SYN for key release : " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
      }
  
      return 0;
  }
  </code></pre>