Event Handling in csharp

S

Sadanand

hi all,
I am Playing with event handlers in C#.
I can able to handle the keys like up arrow, down arrow etc. But when
I press both up and left arrow keys, it is recognizing only up arrow
key. Can anybody please let me know, what is needed to handle that
event? and is it a special case?

Thanks & Regards,
Sadanand.
 
W

Willem van Rumpt

Sadanand said:
hi all,
I am Playing with event handlers in C#.
I can able to handle the keys like up arrow, down arrow etc. But when
I press both up and left arrow keys, it is recognizing only up arrow
key. Can anybody please let me know, what is needed to handle that
event? and is it a special case?

I'm not sure the .NET Framework provides this functionality out of the
box. You could use the WinAPI calls GetKeyState / GetAsyncKeyState /
GetKeyboardState to capture the required information.

With a bit of luck, someone will already have transcribed the signatures
for you. Take a look at http://pinvoke.net, and search for the methods I
mentioned.
 
P

Peter Duniho

Sadanand said:
hi all,
I am Playing with event handlers in C#.
I can able to handle the keys like up arrow, down arrow etc. But when
I press both up and left arrow keys, it is recognizing only up arrow
key. Can anybody please let me know, what is needed to handle that
event? and is it a special case?

You need to handle the KeyDown and KeyUp events, and keep track of which
keys are actually down at any particular time. You won't see more than
one key reported in any given event, but you will see more than one
KeyDown event occur before any KeyUp event occurs, if the user is
pressing more than one key at a time.

Pete
 
S

Sadanand

You need to handle the KeyDown and KeyUp events, and keep track of which
keys are actually down at any particular time.  You won't see more than
one key reported in any givenevent, but you will see more than one
KeyDowneventoccur before any KeyUpeventoccurs, if the user is
pressing more than one key at a time.

Pete

once key up event occurs, at the next moment if another key is still
down, it will not recognize that key down event, is it so? ya this is
what happening for me,
for example I keep on pressing up arrow key , in between i will press
left arrow key, once i release left arrow, and up arrow key is still
down, this moment, even though up arrow key is down,
it will not recognize that key down event.
 
P

Peter Duniho

Sadanand said:
once key up event occurs, at the next moment if another key is still
down, it will not recognize that key down event, is it so? ya this is
what happening for me,

Your description doesn't make sense. You get a KeyDown event at the
moment that the key is pressed. You don't get another one just because
some other key was released.
for example I keep on pressing up arrow key , in between i will press
left arrow key, once i release left arrow, and up arrow key is still
down, this moment, even though up arrow key is down,
it will not recognize that key down event.

There is no KeyDown event to recognize. If you "keep on pressing the up
arrow key", the key remains down. You won't get another KeyDown until
the key has been released (resulting in a KeyUp for that key) and the
key is pressed again.

Pete
 
T

Tim Roberts

Sadanand said:
once key up event occurs, at the next moment if another key is still
down, it will not recognize that key down event, is it so? ya this is
what happening for me,
for example I keep on pressing up arrow key , in between i will press
left arrow key, once i release left arrow, and up arrow key is still
down, this moment, even though up arrow key is down,
it will not recognize that key down event.

No, they all get queued up. In the case you described, you will get the
messages in this order:
"up arrow" down
"left arrow" down
"left arrow" up
"up arrow up
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top