开发者

need help to generate keystrokes into existing application in Linux environment using C++

I need help to generate keystrokes in my existing application in LINUX environment. I am fully aw开发者_JS百科are 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


What you want is create and initialize XKeyEvent struct and send it with XSendEvent.

Be sure to check man xkeyevent and man xsendevent

From man xkeyevent:

   typedef struct {
        int type;                /* KeyPress or KeyRelease */
        unsigned long serial;    /* # of last request processed by server */
        Bool send_event;         /* true if this came from a SendEvent request */
        Display *display;        /* Display the event was read from */
        Window window;           /* ``event'' window it is reported relative to */
        Window root;             /* root window that the event occurred on */
        Window subwindow;        /* child window */
        Time time;               /* milliseconds */
        int x, y;                /* pointer x, y coordinates in event window */
        int x_root, y_root;      /* coordinates relative to root */
        unsigned int state;      /* key or button mask */
        unsigned int keycode;    /* detail */
        Bool same_screen;        /* same screen flag */
   } XKeyEvent;

To get key keycodes, check /usr/include/X11/keysymdef.h ur just use xev

I found a brief introduction for you.

(Edit: It seems someone solved it already :))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜