Hi Folks,

I need help to generate keystrokes in my existing application in LINUX environment. I am fully aware that there are libraries in Dev C++ which do exactly what I want but in Windows and I need something like that in Linux. I have googled a lot, but could not able to find any solution. Below are the code of Dev C++ library and I want something like that in Linux. Any help, suggestion , criticism are most welcome.

void GenerateKey(int vk , BOOL bExtended)
{
KEYBDINPUT kb = {0};
INPUT Input = {0};

// generate down
if(bExtended)
kb.dwFlags = KEYEVENTF_EXTENDEDKEY;
kb.wVk = vk;

Input.type = INPUT_KEYBOARD;
Input.ki = kb;
::SendInput(1, &Input, sizeof(Input));
// generate up
::ZeroMemory(&kb, sizeof(KEYBDINPUT));
::ZeroMemory(&Input, sizeof(INPUT));

kb.dwFlags = KEYEVENTF_KEYUP;
if(bExtended)
kb.dwFlags |= KEYEVENTF_EXTENDEDKEY;

kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
::SendInput(1, &Input, sizeof(Input));
}

Thanks and regards, SamPrat
c++ linux

Hi,

It can be done in combination of some linux functions:

Here is a code snippet should work on every linux platform, it will take all the keystrokes and when a ctr+c is pressed, it will terminate.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>

int kbhit(void)
{
  struct termios oldt, newt;
  int ch;
  int oldf;

  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

  ch = getchar();

  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);

  if(ch != EOF)
  {

    //DO whatever you like to do with this charecter ..

    ungetc(ch, stdin);
    return 1;
  }

  return 0;
}


main( int argc, char** argv )
{
        char *input = argv[0];
        int nomor = argc;
        pid_t pid = 0;

        int x=0;
        while(1)
        {
                if(kbhit()){
                        //clearing the buffer
                        char ch = getchar();
                        printf("you hit keyboard and key = %c\n", ch);
                }
        }
}

I just copied it from this link and made little bit changes (got rid of fork).
If you want the fork, see the following link:

kbhit()

Dear alwaysLearning0,
Thanks for the reply.

But I guess , your function is capturing the key event. Though what I want is to simulate the keystrokes.
I have found some solution to generate thekey stroke by looking into lib /usr/include/X11/keysymdef.h
SO I am trying to implement with it.

Dear Jainpratik2
I have the same problem with simulating keystrokes under Linux.
Yoe wrote,that you tried to solve the problem.
Can you please give me a code snipped of your solution.
Thanks
Linux_freak

Though I am unfamiliar with the exact approach, this can be done using Xlib. It looks like createKeyEvent is what you want to look at (it uses the definitions in the header you listed)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.