LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to press Alt + F1 keys programmatically in Linux (http://www.linuxquestions.org/questions/programming-9/how-to-press-alt-f1-keys-programmatically-in-linux-552490/)

dipk_23 05-09-2007 01:44 AM

How to press Alt + F1 keys programmatically in Linux
 
Hi all,
In one of my module, i need to simulate Alt+F1, Alt+F2 .... Ctrl+F1 etc keys programmatically. I m using XLib/X11 library. i has been tested following code in my machine (Suse 10.2)..
void AltPress() {
XKeyEvent event; // The structure for KeyPress or KeyRelease
Display *mpXServerDisplayObject = XOpenDisplay (NULL);
event.display = mpXServerDisplayObject;
int Return_Value;
XGetInputFocus ( mpXServerDisplayObject,&event.window,&Return_Value);
Screen *scr = DefaultScreenOfDisplay(mpXServerDisplayObject);
event.root = RootWindowOfScreen(scr);
event.subwindow = None;
event.time = 0;
event.x = 1;
event.y = 1;
event.x_root = 1;
event.y_root = 1;
event.same_screen = TRUE;
event.type = KeyPress;
event.keycode = 0xffbf; // code for F1 key
event.state = XKeycodeToKeysym (mpXServerDisplayObject, 113, 0);//code for Alt Key
XSendEvent(event.display, event.window, TRUE, KeyPressMask, (XEvent *)&event);
XFlush(mpXServerDisplayObject);
}

Any suggestion.?????

osor 05-09-2007 06:06 PM

I can’t quite make out where the error is. Personally, however, I would use the XTest framework rather than dealing with the complexities of XSendEvent. Basically, you use XTestFakeKeyEvent to send a keydown event for “Alt”. Then use XTestFakeKeyEvent to send a keydown event for F1. Then send keyup events for F1, followed by a keyup event for “Alt”. If you need help with the actual code, just post.

jlliagre 05-09-2007 06:26 PM

Indeed, I posted that XTest based code recently for a very similar request:
http://www.linuxquestions.org/questi...17#post2729917

dipk_23 05-10-2007 12:59 AM

Quote:

Originally Posted by jlliagre
Indeed, I posted that XTest based code recently for a very similar request:
http://www.linuxquestions.org/questi...17#post2729917


Thanks a lot for this sample...

dipk_23 05-10-2007 01:01 AM

Quote:

Originally Posted by osor
I can’t quite make out where the error is. Personally, however, I would use the XTest framework rather than dealing with the complexities of XSendEvent. Basically, you use XTestFakeKeyEvent to send a keydown event for “Alt”. Then use XTestFakeKeyEvent to send a keydown event for F1. Then send keyup events for F1, followed by a keyup event for “Alt”. If you need help with the actual code, just post.


U r right... Thanks for help..


All times are GMT -5. The time now is 07:19 PM.