ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
After trying for days, I really don't know how to solve my problem:
I need to DIRECTLY POLL the keyboard under my Linux C++ (gcc) program. The program will run under ROOT priviliges in USER-space. Because I use the QT toolkit for the user-interface, the standard input is not available.
Multithreading is not an option: It is absolutely neccesary that there will only be one thread in the program, since my program will be locked into memory (mlockall) and run realtime (sched_setscheduler -> SCHED_FIFO -> highest_priority). While the program truly blocks all other processes, all I do is simple I/O instructions (inline assembly) and filling an array of values. But the problem is, I need to catch the exception that the user presses Escape.
Solutions I have tried (and thought of) so far:
- Polling the I/O port 0x60 (keyboard port), this works, but I guess it only works when I have a PS/2 keyboard connected... So USB-keyboards won't work I think, CAN ANYONE AFFIRM THIS??, since I don't have a USB-keyboard at the moment available.
- BIOS calls : not supported in Linux (nor with root-priviliges)
Anyone knows another way of directly polling the keyboard? System calls maybe? How do I do this?
Remember: My program doesn't need to be portable/compatible with other platforms. It just needs to run on Linux (preferably 2.6). (With root-priviliges)
In the SDL library, you can use the functions SDL_GetKeyState and SDL_GetModState to get the current state of any of the keys at a specific time. Not sure if QT has something similar or not, but if it doesn't, you might still be able to use just these SDL functions.
Hi FredKroket, :-) My post is not intended to answer your question. It is my ideas of what I must do if I were in you situation:
I am not a linux/low-level programmer but if I understand your need, you have no choice but to use the BIOS's keyboard input queu addresses. <-- That means you have to write your own kernel module because those addresses ( If I remember the queu size is 15x16 bits integers ) are not accessible in user space indeed. It should not be difficult though... But if doing that you must put the CPU in real mode to access the bios array addresses ...( ouch ! )
That is true: USB keyboards are not accessed by traditional IO PORTS indeed - its USB :-)
If the BIOS is not an option because of protected mode vs real mode, then I think, still writing a custom kernel module, you are left to use the USB IO....
I would like to say that there are USB keyboard drivers modules accessible dynamicaly to be usable in user-space... I dunno tho.
I will resort to the IO 0x60 port polling method I guess, since this is the only way to poll the keyboard really fast, without using kernel/library functions at all..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.