Detect if any key is pressed in C# (not A, B, but any)?

DllImport("user32. Dll", EntryPoint = "GetKeyboardState", SetLastError = true) private static extern bool NativeGetKeyboardState(Out byte keyStates); private static bool GetKeyboardState(byte keyStates) { if (keyStates == null) throw new ArgumentNullException("keyState"); if (keyStates. Length!

= 256) throw new ArgumentException("The buffer must be 256 bytes long. ", "keyState"); return NativeGetKeyboardState(keyStates); } private static byte GetKeyboardState() { byte keyStates = new byte256; if (!GetKeyboardState(keyStates)) throw new Win32Exception(Marshal. GetLastWin32Error()); return keyStates; } private static bool AnyKeyPressed() { byte keyState = GetKeyboardState(); // skip the mouse buttons return keyState.

Skip(8). Any(state => (state & 0x80)! = 0); }.

Thanks for the effort, but I get a "EntryPointNotFound" exception. May be due to windows 7 restrictions, different kernel, don't know. Also, to low level (even if this would be the only way)... – Christian Nov 18 '09 at 0:17 @Christian: note that I originally had the wrong import specified - I put kernel32 but it's supposed to be user32.

– 280Z28 Nov 18 '09 at 0:45 @Christian: also, I added a Skip(8) to AnyKeyPressed so it doesn't return true due to pressing mouse buttons. – 280Z28 Nov 18 '09 at 0:46 Ok, nice, it works! One more question, how could I "filter" the CTRL, ALT, Windows and SHIFT.

Because they should not count as keys, I handle them using the ModifierKeys (design issue, would require a lot of changes otherwise). Is this possible? Thanks a lot in any case.. – Christian Nov 18 '09 at 1:48 @Christian: You can check all the bytes except the ones you want to ignore.

If you are ignoring the modifiers, are you also ignoring VK_NUMLOCK, etc. (this checks down, not toggled, but what do you do if the numlock key is actually down)? Virtual key code reference: msdn.microsoft.Com/en-us/library/ms927178. Aspx – 280Z28 Nov 18 '09 at 1:51.

Normally I would use an event You should still be using an event, preferably KeyDown since you don't mind what key is pressed, if you are programming on windows. If not, you could use something like Console.ReadLine();. Edit: If you are looking for something like if (Keyboard.

IsKeyDown(Key. AnyKey)) return true; then you must be joking... edit 2: Well, the approach of your logger is interesting, but I think that you are reinventing the wheel. All programming languages provides some way to handle wich key was pressed, when this can be known.In C# for Windows, this is done using events.

Besides, I think you won't be able to handle this kind of things by yourself in . NET, since you need to access some system functions from the Win32 API, and AFAIK you aren't allowed to do this (at least easily...) in managed code. Some solution would be to create a HOOK and send messages from there to your application, but I don't know how to do this in C#.

This was the way in Delphi, where I have more experience.

Not joking :-) But obviously wrong train of thought. But I don't find it THAT far fetched. I mean why not, there might be cases if I want to know if ANY key is pressed.

Edited the entry, will try the static "logger" which can be used for checking... – Christian Nov 17 '09 at 23:28.

Many forms and controls will fire a KeyPress event as well as a KeyDown event. You may want to look into those events and set your flag to true when one of them occurs. You'd also have to listen for the corresponding event that tells you when the key is released (KeyUp, also, I think).

If you're using Windows. Forms, use the KeyDown event and read out the specific key using the appropriate KeyEventArgs. You can access the KeyCode property on the KeyEventArgs variable.To check for ranges, say between A and Z: if (e.

KeyCode>=Keys. A && e.KeyCode.

sourceforge.net/projects/keystate/ shows "special" keys. Otherwise I think you're just going to have to monitor them. SOunds like you want to do this systemwide?

What are you trying to accomplish?

You can increment a counter for each keydown event, and decrement it for each keyup event. When the counter is zero, no keys are down.

Not possible, KeyDown events get eaten by some WPF elements, and I may not even be in my app when pressing A. Then I hold it, click on my app, and it does not check that A is pressed... – Christian Nov 17 '09 at 23:38 1 You're watching a house. A person goes in.

A person goes out. A person goes in. A person goes out.

A person goes out. What do you logically conclude about this sequence of observations? (1) there are -1 people in the house, or (2) there were people in the house before I started watching?

– Eric Lippert Nov 18 '09 at 2:15 @Eric: I would say that if someone now goes in, the house will become empty. – Zano Feb 18 '10 at 14:14.

See this question : Capture KeyDown Event in WPF.

Yes, this makes my "static key tracker" approach not very feasible. At least it seems overcomplicated for such a "simple" thing like "tracking keysets on mouse clicks". See edit2 in post.

– Christian Nov 17 '09 at 23:36.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions