Keyboard Buffer Insertion

This is a discussion on Keyboard Buffer Insertion within the C Programming forums, part of the General Programming Boards category; I've been asked (by my brother) to create a program which will insert a specific key combination into the keyboard ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30

    Keyboard Buffer Insertion

    I've been asked (by my brother) to create a program which will insert a specific key combination into the keyboard buffer at specific intervals until the Esc key is pressed.

    Does anyone know how to insert a 'keypress' into the keyboard buffer, as this is what I'm having difficulty with.

    Thanks in advance...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Why do you (or perhaps your brother) want to do this?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    It's to control another program. The other program requires him to press a key repeatedly, and he is looking for a way of doing this that doesn't require him to sit at his keyboard.

    There are lots of keypress and mouse-click programs available on the internet, but these are often designed for cheating in browser-based games, and often contain malware. He was hoping that I'd be able to create a bespoke program for him that does a similar thing, but that he knows he can trust (as I'll also give him the source code).

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    AutoHotKey is a program that does what you're looking for. Since it's open source, your brother can trust (or verify) that it doesn't contain malware.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    Thanks bithub, I've passed the link on to my brother.

    I'm still interested in creating a simple bespoke version for him, and have thrown together some basic functions. Please note that this is currently untested mailer code right off the top of my head, so may not compile cleanly.

    Code:
    #include <stdio.h>
    #include <dos.h>
    
    typedef union _Keypress
    {
    	int iKeyCode;
    	char ch[2];
    } KEY;
    
    #define KEYBOARD    0x16
    #define KEYCODE     iKeyCode
    #define SCANCODE    ch[1]
    #define ASCIICODE   ch[0]
    
    #define NO_KEY         0
    #define ESCAPE_KEY     1
    
    int insert_keypress(KEY k)
    {
      union REGS sreg,dreg;
    
      sreg.h.ah = 5;           /* Set 'Store' Function */
      sreg.h.ch = k.SCANCODE;  /* Set Key Scancode     */
      sreg.h.cl = k.ASCIICODE; /* Set Key ASCII Code   */
      
      int86(KEYBOARD, &sreg, &dreg);
      
      return ( dreg.h.al );  /* Success (1) or Fail (0) */
    
    }
    
    int get_keypress(void)
    {
      KEY symKey;
      union REGS sreg,dreg;
    
      sreg.h.ah = 0;           /* Set 'Get' Function */
      
      int86(KEYBOARD, &sreg, &dreg);
    
      k.SCANCODE  = dreg.h.ah; /* Get Key Scancode     */
      k.ASCIICODE = dreg.h.al; /* Get Key ASCII Code   */
      
      return k.KEYCODE;
    }
    
    int check_keypress(void)
    {
      KEY k;
      union REGS sreg,dreg;
    
      sreg.h.ah = 0;           /* Set 'Check' Function */
      
      int86(KEYBOARD, &sreg, &dreg);
    
      /* Check the zflag (0=nothing in queue, 1=key pressed) */
      if ( regs.x.cflags & 0x0040 ) {
        k.KEYCODE   = 0;         /* Nothing in queue     */
      } else {
        k.SCANCODE  = dreg.h.ah; /* Get Key Scancode     */
        k.ASCIICODE = dreg.h.al; /* Get Key ASCII Code   */
      }
      return k.KEYCODE;
    }
    
    void flush_kb_buffer(void)
    {
      KEY symKey;
      while (check_keypress()) {
        symKey.KEYCODE = get_keypress();
      }
    }
    
    void main(void)
    {
      KEY k, temp, ;
    
      flush_kb_buffer();
      
      printf("Enter key to repeat:");
    
      k.KEYCODE = get_keypress();
      
      while (k.SCANCODE != ESCAPE_KEY) {
        if ((temp = check_keypress()) == 0) {
          /* Nothing in buffer queue, put something in */
          printf("Inserting Keypress\r\n");
          insert_keypress(k);
        } else {
          /* We have something in the buffer, grab it */
          k.KEYCODE = get_keypress();
        }
      }
    
      printf("Goodbye!\r\n");
    }
    You will also notice that I'm using the INT16h DOS interrupt, and function 05h to insert the keypress. Apparently this worked on some, but not all old PC's, and probably won't work on modern ones. Does anyone know another approach I can use.

    The get_keypress and check_keypress functions I wrote above should work exactly like the old obsolete bioskey(0) and bioskey(1) functions if you wanted to look them up. These should be compatible with modern Windows-based PC's.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Anytime you see "#include <dos.h>", it's probably a good sign that it won't work on a modern PC. You should be using the Win32 API instead.

    If I remember correctly, you need to call SendMessage() to send the WM_KEYDOWN message to the window that is receiving the keyboard input.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  3. Send output to keyboard buffer
    By DReynolds in forum C Programming
    Replies: 2
    Last Post: 06-06-2007, 03:44 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. : Difference between message queue and keyboard buffer...
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 02-21-2002, 02:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21