You are not logged in.

#1 2011-05-12 10:39:46

thelastblack
Member
Registered: 2010-11-14
Posts: 43

help with a program

hi
i want to write a program which sends the whole keyboard keys to a program(active one);
i tried a script like this

str="hdvnlki,jfsa;cg'rtou;p[]qxwyzebm\Sh\Sc";
while (true) do
    crikey $str;
    sleep 0.1;
done

i used both crikey and xvkbd for sending key, i found crikey is faster...
but i want a faster way, i wanted to write a c++ programs for that but it doesnt seem to work...
and besides, what if i want to send non-english key? i want to send persian keys and just found their name in "keysymdef.h" , but how should i use them?
i used a c++ code like this:

// Send a fake keystroke event to an X window.
// by Adam Pierce - http://www.doctort.org/adam/
// This is public domain software. It is free to use by anyone for any purpose.

#include <X11/Xlib.h>
#include <X11/keysym.h>

// The key code to be sent.
// A full list of available codes can be found in /usr/include/X11/keysymdef.h
#define KEYCODE (XK_Arabic_superscript_alef,XK_Arabic_tteh,XK_Arabic_peh)

// Function to create a keyboard event
XKeyEvent createKeyEvent(Display *display, Window &win,
                           Window &winRoot, bool press,
                           int keycode, int modifiers)
{
   XKeyEvent event;

   event.display     = display;
   event.window      = win;
   event.root        = winRoot;
   event.subwindow   = None;
   event.time        = CurrentTime;
   event.x           = 1;
   event.y           = 1;
   event.x_root      = 1;
   event.y_root      = 1;
   event.same_screen = True;
   event.keycode     = XKeysymToKeycode(display, keycode);
   event.state       = modifiers;

   if(press)
      event.type = KeyPress;
   else
      event.type = KeyRelease;

   return event;
}

main()
{
// Obtain the X11 display.
   Display *display = XOpenDisplay(0);
   if(display == NULL)
      return -1;

// Get the root window for the current display.
   Window winRoot = XDefaultRootWindow(display);

// Find the window which has the current keyboard focus.
   Window winFocus;
   int    revert;
   XGetInputFocus(display, &winFocus, &revert);
int j[3];
j[0]=XK_Arabic_superscript_alef;
j[1]=XK_Arabic_tteh;
j[2]=XK_L;
   for(int i=0; i<3 ; i++) {
// Send a fake key press event to the window.
   XKeyEvent event = createKeyEvent(display, winFocus, winRoot, true, j[i], 0);
   XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);

// Send a fake key release event to the window.
   event = createKeyEvent(display, winFocus, winRoot, false, j[i], 0);
   XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
}

// Done.
   XCloseDisplay(display);
   return 0;
}

i want a more optimized way to do that cause when i run the script my cpu load in more than 70% on both cores.. i know its because of inf loop.
thx in advance and please, please note that i am in hurry. so...
thx

Offline

#2 2011-05-12 22:48:24

thelastblack
Member
Registered: 2010-11-14
Posts: 43

Re: help with a program

ok, forget about those things now,
how could i send unicode characters to a programs? crikey and xvkbd have problems with them(or i dont know them?)
any ideas?
thx

Offline

Board footer

Powered by FluxBB