views:

58

answers:

1

I want to check if the user pressed down the SHIFT key, when program starts. (That means, press down the SHIFT key before the program is started) It's a simple console program, nothing related to X.

This maybe similar to Win32 GetKeyboardState() function.

I want to know whether I can do this and how, but not any pros and cons with accessing the terminal directly.

A: 

You can't.

The shift key isn't considered as a character key, so even if you access the terminal directly, you won't be able to detect this key.

Maybe you shouldn't have to. Imagine for example that you are using a US keyboard where numbers are accessible on the top row without modifiers, and also checking for the shift key. People with other keyboard layout may have to use shift modifiers to access the numbers. If your program react to this shift press, then your program is basically unusable. The same thing applies for other modifier keys : you may detect some of them only after a normal character key is pressed. Or worse, they may need to use the shift key to use 'enter' to run your program.

Also, what shift key do you want to monitor ? the one on the local machine, or the one where the user is ? remember that SSH exists and is commonly used to access a pseudoterminal remotely.

If you are root and want to monitor the Shift key on the local machine, you can read the evdev devices for events about the shift key. But this is only possible because of automatic key repeating, so you won't detect a shift key that is pressed right before running your program, but only a few second before.

Of course you can't do that on the remote machine, that would be a security flaw.

And anyway, why would you want to do that ? wouldn't an X application be the right thing to do in your case ?

BatchyX
It's a general purpose command invoker, while _SHIFT_ key is held, the invoker will enter into configuration mode. Though there's other way to do this, for example a command line `--conf` option, well, knowing it's impossible is enough, I'll try it other way.
谢继雷