LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 02-07-2021, 02:58 AM   #1
doom_23
LQ Newbie
 
Registered: Feb 2021
Location: Poland
Distribution: Debian Sid
Posts: 12

Rep: Reputation: Disabled
Do a function until a key is pressed.


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
 
Old 02-07-2021, 05:47 AM   #2
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,918

Rep: Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035
My advice would be make the key work like a toggle. There's no good way to determine if a key is being held down in a tty/terminal program.
 
Old 02-07-2021, 06:21 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,627

Rep: Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556

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.

 
Old 02-07-2021, 11:04 AM   #4
doom_23
LQ Newbie
 
Registered: Feb 2021
Location: Poland
Distribution: Debian Sid
Posts: 12

Original Poster
Rep: Reputation: Disabled
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?
 
Old 02-07-2021, 12:17 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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.
 
Old 02-16-2021, 10:16 PM   #6
vmelkon
Member
 
Registered: Feb 2007
Location: Canada
Distribution: Kubuntu 22.04
Posts: 549

Rep: Reputation: 84
Quote:
Originally Posted by NevemTeve View Post
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.

~~~~vmelkon
 
Old 02-18-2021, 05:43 AM   #7
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 442

Rep: Reputation: 196Reputation: 196
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)
 
Old 02-18-2021, 07:20 AM   #8
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,918

Rep: Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035
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).
 
Old 02-18-2021, 08:24 AM   #9
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 442

Rep: Reputation: 196Reputation: 196
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.
 
  


Reply



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
[SOLVED] Keyboard doesn't sense key strokes if key kept pressed yogesh_attarde Linux - Software 1 06-24-2014 09:28 AM
Keyboard Doesn't Respond to input until Menu Key is pressed - Ubuntu 11.04 AlexOnVinyl Linux - Newbie 1 09-04-2011 09:51 PM
Map a keypress to a GUI button (when keycode 175 pressed a GUI button pressed) wikapuki Linux - Desktop 0 09-16-2010 09:08 AM
Opera often hanging until "stop" pressed Wombat Pete Linux - Software 4 04-30-2010 08:55 AM
Which ioctl to use to get the key pressed in the key board? Sreeram B S Linux - Kernel 0 02-24-2007 02:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:38 PM.

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
Open Source Consulting | Domain Registration