LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-09-2007, 02:44 AM   #1
dipk_23
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Rep: Reputation: 0
Question 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.?????
 
Old 05-09-2007, 07:06 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
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.
 
Old 05-09-2007, 07:26 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Indeed, I posted that XTest based code recently for a very similar request:
http://www.linuxquestions.org/questi...17#post2729917
 
Old 05-10-2007, 01:59 AM   #4
dipk_23
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Original Poster
Rep: Reputation: 0
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...
 
Old 05-10-2007, 02:01 AM   #5
dipk_23
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Original Poster
Rep: Reputation: 0
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..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
keyboard issue - have to double press some keys whysyn Linux - Hardware 9 10-31-2005 09:25 AM
How do you press Ctrl,Alt and shift? littertiger Programming 4 05-17-2005 04:00 AM
Keyboard Problem - I have to press a key twice for certain keys daedius Linux - Hardware 4 01-13-2005 07:32 AM
Can Only Press 3 or 4 Keys At Once While Gaming soroh Linux - Hardware 3 07-14-2004 03:33 AM
Beep when I press keyborad keys.. ! xconspirisist Fedora 1 01-30-2004 07:37 PM


All times are GMT -5. The time now is 02:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration