X11: convert a string into keysym


 
Thread Tools Search this Thread
Top Forums Programming X11: convert a string into keysym
# 1  
Old 01-17-2012
X11: convert a string into keysym

Dear all,
i need to send fake key events to my application, where the keysym to send is readen from a config file and saved to a vector<string>.
I use:
Code:
 
[...]
#include <X11/Xlib.h>  
#include <X11/Intrinsic.h>  
#include <X11/extensions/XTest.h>  
#include <unistd.h>  

//the below function from "http://bharathi.posterous.com/x11-fake-key-event-generation-using-xtest-ext"

static void sendKBEvent (Display * display, KeySym keysym, KeySym modsym)
{
  KeyCode keycode = 0, modcode = 0;
  
  keycode = XKeysymToKeycode (display, keysym);
  
  if (keycode == 0) return;

  XTestGrabControl (display, True);

  /* Generate modkey press */
  if (modsym != 0)
  {
     modcode = XKeysymToKeycode(display, modsym);
     XTestFakeKeyEvent (display, modcode, True, 0);
  }

  /* Generate regular key press and release */
  XTestFakeKeyEvent (display, keycode, True, 0);
  XTestFakeKeyEvent (display, keycode, False, 0);

  /* Generate modkey release */
  if (modsym != 0)
    XTestFakeKeyEvent (display, modcode, False, 0);

  XSync (display, False);
  XTestGrabControl (display, False);
}


int main(int argc, char** argv)
{
   [...]
   vector<string> kbdEventsVect;

   ConfigFile configFile;
    
   kbdEventsVect = configFile.parseFile("file.txt");
   
   /*kbdEventsVect[] contains XK_Up, XK_Down, XK_Escape, XK_Right*/

   [...]
   sendKBEvent(display, kbdEventsVect[0], 0);
  [...]
 }

the problem is that the compiler does not like me passing a string ( kbdEventsVect[0]) to
sendKBEvent(), which instread needs a Keysym var as 2 argument.

Code:
error: cannot convert ‘std::basic_string<char,   std::char_traits<char>, std::allocator<char> >' to   ‘KeySym' for argument ‘2' to ‘void sendKBEvent(Display*, KeySym,   KeySym)'

The wired thing is that if I pass directly the string, everything works fine

Code:
   sendKBEvent(display, XK_F11, 0);

but If I use &kbdEventsVect[0] == XK_F11

Code:
sendKBEvent(display, kbdEventsVect[0], 0);

I guess i'm missing something...

Thank you for any help you would give me.

Martina
# 2  
Old 01-17-2012
XK_F11 isn't a string, a string would be "XK_F11". It's a #define which becomes an integer, in the end. See xorg/proto/xproto - X.org X11Proto protocol headers.

Fortunately you can turn a string into a keysym with XStringToKeysym().

As far as I know it ought to be as simple as
Code:
int key=XStringToKeysym("XK_F11");

# 3  
Old 01-18-2012
X11: convert a string into keysym

Quote:
Originally Posted by Corona688
XK_F11 isn't a string, a string would be "XK_F11". It's a #define which becomes an integer, in the end. See xorg/proto/xproto - X.org X11Proto protocol headers.

Fortunately you can turn a string into a keysym with XStringToKeysym().

As far as I know it ought to be as simple as
Code:
int key=XStringToKeysym("XK_F11");

Thank you for your reply, now it is much clear Smilie
The fact is that I need to send to sendKBEvent(display, keysym, keysym) a string retrieved from a vector (filled with data obtained from a configFile)
Code:
vector<string> kbdEventsVect;

   ConfigFile configFile;
    
   kbdEventsVect = configFile.parseFile("file.txt");
/*  
     kbdEventsVect[0] = XK_Up
     kbdEventsVect[1] = XK_Down
     kbdEventsVect[2] = XK_Escape
     kbdEventsVect[3] = XK_Right
*/

   [...]
   sendKBEvent(display, kbdEventsVect[0], 0);
  [...]

so if I do
Code:
int key=XStringToKeysym(kbdEventsVect[0]);
   
   sendKBEvent(display, key, 0);

I obtain from the compiler:
Code:
cannot convert ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to ‘const char*' for argument ‘1' to ‘KeySym XStringToKeysym(const char*)'

I guess because XStringToKeysym is expecting a char array and instead gets a stl string, right?
What would you suggest me to do?
# 4  
Old 01-18-2012
Code:
int key=XStringToKeysym(kbdEventsVect[0].c_str());

# 5  
Old 01-23-2012
X11: convert a string into keysym

Quote:
Originally Posted by Corona688
Code:
int key=XStringToKeysym(kbdEventsVect[0].c_str());

... I'm finally able to access the sendKBEvent() function, but the conversion function XStringToKeysym() just maps all my strings to zero.
I guess the problem is that the strings read from the config file
Code:
kbdEventsVect[0].c_str() = XK_Up
kbdEventsVect[1].c_str() = XK_Down
[..]

are not corectly mapped to the corresponding keysym code, as obtained from X11/keysymdef.h header file.
Is there a simple way to get the correct codes or shall I write a mapping class ?
Thank you for your help!
# 6  
Old 01-23-2012
If they're not the right key codes, I don't know what reading them from the file would accomplish.

Have you tried converting them to all-uppercase before feeding them into function?

You could also try stripping off the XK_ from the front.

Keep trying different combinations until you have something X11 will accept. I don't have a machine running X to test with.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 01-23-2012
X11: convert a string into keysym

Quote:
If they're not the right key codes, I don't know what reading them from the file would accomplish.
Sorry, i was not so clear. My vector contains just the "mnemonic macro names for these keysyms" (from X11/keysymdef.h), i.e.
Code:
kbdEventsVect[0] = XK_Up 
kbdEventsVect[1] = XK_Down
[...]

In X11/keysymdef.h
Code:
#define XK_Up                            0xff52
#define XK_Down                        0xff54

So my goal is to make my code read the kbdEventsVect[0], and map the corresponding string (XK_Up) to the correct int (0xff52). My problem is that the last step does not want to work, since my code does not read the string XK_Up to the correct macro in the X11 library.
Anyway, I found a loophole, not what I wanted but for know it is fine. I directly fill my string vector with the keysym codes (0xff52), so

Code:
kbdEventsVect[0] =0xff52

then I convert the string into an unsigned int
Code:
 unsigned int kbdKeyNum;

 kbdKeyNum = strtoul (kbdEventsVect[0].c_str(),NULL,0);
   
 sendKBEvent(display,kbdKeyNum, 0);

and now it works.

Thank you for your help!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert a String to a Number

I read in two numbers from a user but the number is a string. #!/bin/bash read -p "Enter first number: " num1 read -p "Enter second number: " num2 I know you can use the the "expr" or "bc" command to automatically convert the string to a number then add them together. But I don't want to add... (10 Replies)
Discussion started by: Loc
10 Replies

2. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

3. Shell Programming and Scripting

Convert string using sed

I have a couple structure definitions in my input code. For example: struct node { int val; struct node *next; }; or typedef struct { int numer; int denom; } Rational; I used the following line to convert them into one line and copy it twice. sed '/struct*{/{:l... (3 Replies)
Discussion started by: James Denton
3 Replies

4. UNIX for Advanced & Expert Users

[Solved] putty+x11:How do I pass X11 display rights to "su"?

I can log into a unix system with Putty. I've set the "X11 forwarding" checkbox, and I've verified that I can display an X11 window back on my laptop. What I need to be able to do is "su" to another uid after logging in and then run something which display a window back on my laptop, with the... (2 Replies)
Discussion started by: dkarr
2 Replies

5. Shell Programming and Scripting

Help with convert string

Hi. I will be very appreciated for help. I need replace all characters into string with \ (backslash) I mean if I have word abcdefg as input. How I can convert it to \a\b\c\d\e\f\g Thanks and best regards. Staas. (5 Replies)
Discussion started by: beckss
5 Replies

6. Shell Programming and Scripting

how to convert array into the string

Hi I have a line/string as follows: A=" 3498 NORDEA - INDX LINKED NORY" which was converted into an array of characters: p321$ echo "${ARR}" 3 4 9 8 N O R D E A - I N D X L I N K E D N O R Y When I am trying print this array there are blank... (4 Replies)
Discussion started by: aoussenko
4 Replies

7. Windows & DOS: Issues & Discussions

cygwin-x/can't install xorg-x11-f100 & xorg-x11-fnts

Hello All. Really a newbie to Linux/Unix. Trying to get into Linux. Using XP PE currently. Installed cygwin and trying to intall cygwin-x. Everything else is setup nice but i can't seem to install these two packages (without whom xwin won't start) 1. xorg-x11-f100 2. xorg-x11-fnts Tried the... (1 Reply)
Discussion started by: binodbdrchand
1 Replies

8. Shell Programming and Scripting

how to convert a string to int

Hi, i want to read a text file whose content(single line) will be a number like 1 or 2 or 3 ..... what i want to do is to read the file and increment the content of the file, using unix scripting. Regards, Senthil Kumar Siddhan. (2 Replies)
Discussion started by: senthilk615
2 Replies

9. Shell Programming and Scripting

Convert string to numeric

Hi, I have read some figures from a text file by getting the position and wish to do some checking, but it seem like it won't work. eg. my figure is 0.68 it still go the the else statement, it seems like it treat it as a text instead of number. Anybody can Help ? Thanks. # only... (3 Replies)
Discussion started by: kflee2000
3 Replies
Login or Register to Ask a Question