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.
Hello. This is my first post here
I develop a little tool in C++ and I need to run some function while a key is pressed. I tried to use the getch() function and it works but if I am pressing a key, function calls 1 time, then I have wait until the "keyboard delay" will pass (I have 500ms delay in my xfce settings), and only then function will looped.
But how can I avoid this 500ms delay and immediately run a function?
UPD: Forget to say that I am using ncurses
Last edited by doom_23; 02-07-2021 at 10:08 AM.
Reason: spelling
As GazL suggests, terminal deals with characters, rather than keyboard events. (The latter is what tells you when a key is pressed/released, also called key down/up.)
Though you've not specified that you need to be using the terminal, so you may still be able to do what you want.
Running "xev -event keyboard" will open a window and print information about KeyPress/KeyRelease events that occur when that window is focused - maybe check the xev source to see how it does it, and whether your software can use similar code.
Also I use ncurses so I am trying to find some methods of working with keyboard/mouse events but I can't really useful info. Maybe I should use an extended library that will check input events?
I think an easy way to write a X Windows application is writing a Java GUI-application. Bonus: it will work on other platforms, too.
You can also use Qt Creator as your IDE and use the Qt Libraries to make a windowed GUI application. It is also cross platform. You would write your app in C++.
I don't know how to get the keyboard state with Qt.
With DirectX, there is DirectInput and there is a single function for getting keystates of all keys in one shot.
Also, DirectInput is old and dead.
Why would anyone let go of the key once it is pressed down?
(In other words, if the reason is something the computer can give feedback on, use that as a terminator, not a held down key that could be meddled with in various ways, like a sneeze or a moth flying into the users eye)
Here's a couple of examples of why one might care about seeing key press/release:
Back the the day, there was a very nice DOS file-manager program called XTree. It used to display hints on screen to show what each hotkey would do, and the hints would change to reflect their new meaning when one held down a modifier key (shift/ctrl/alt) to show that pressing the key in combination with that modifier would now do something different. Although there have been a number of attempts to create an XTree clone for ncurses over the years, non of them have been able to duplicate this feature because tty programs can't see keyboard events (only input characters).
Another example might be a volume/brightness control where one might want the volume to increase/decrease while the key is held down (to save the user having to repeatedly tap the key). Again, this is not something that comes easy to tty/ncurses apps as one would have to rely on key-repeat (and accept the initial delay that entails).
Yeah, I can see that application, I asked the question in case there's something that could be automated instead of a nebulous "held key" problem.
If it was something like a contrast or brightness question then I'd guess that OP might have added that.
When there's nebulous questions I tend to "make an ass out of them and me" by assuming it's some sort of hacky kludge since actual "Zer0 C00l hackerman" would know basic programming and probably wouldn't need help on his top secret elite gibson hacking program that noone must know what it is does (the nebulosity).
So maybe it's just some hacky cludge that still has some nefariousness to it, or something embarrasing or whatever, and those things could possibly be computer detactable with some more skill (like properly parsing network packets or reading the feed of a heat sensing camera that triggers the victory jingle when the observed admins blood pressure and body temp has risen sufficently at the attack or something)
That said, held down adjustments tend to be iffy in my opinion, too. Especially if the speed ramps up the longer one holds the key and then shoots past the goldilocks zone and then one has to mash buttons anyway.
Going for a middle ground default that cuts travel in half is probably better in that case, and then fine tuning from there.
Or since the keyboard has a lot of keys, make some go in chunks, some keys go in little ticks.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.