How to detect multiple simultaneous key presses? And a threadquestion

R

raylopez99

Compound question: first, and this is not easy, if there's a way to
detect multiple simultaneous key presses in C# let me know (in the
below code, keys c and d being pressed simultaneously or nearly so).
I researched this and for C# (as opposed to MFC) there is no library
function, and no easy way, though some code on the net suggested that
you set up a thread that 'lives' for a certain time, then, if keys are
pressed in that certain time, you set a magic number or bool (global)
to detect this condition.

So, I tried this but failed (multiple versions--one nasty one was an
infinite loop that required rebooting). I guess I don't know enough
about how threads work. Below is some code--what am I doing wrong?

class 'KeyHelperClass' is a nested internal class that is declared
inside the form, Form1. I instantiate it in the form construction,
but I think the solution is to somehow instantiate it in the event
handler method "OnKeyDown" using a 'thread' so that it's "alive" for
only say 500ms. But how to get "OnKeyDown" to fire more than once,
without the thread (and the KeyHelperClass) disappearing? maybe
there's somewhere else to put it?

BTW this code ' trivially works' if you press c and d keys
sequentially--and once they are sequentially pressed then do what you
want, then reset the 'magic number' or bool. You don't even need
threads to do this. But to learn more, I'd like to learn if you can
do this using 'threads', to detect simultaneous (or near simultaneous,
say within 500 ms), otherwise, the workaround solution is rather
trivial.

BTW for future reference I've read on the net some old keyboards do
not detect more than three keys being depressed.

RL

//KeyHelperclass declared as 'global' to form.

The below lines are inside of private void Form_KeyDown(object sender,
KeyEventArgs e) event handler //is this the right place?

//

ThreadStart threadDelegate = new
ThreadStart(myKeyHelperClass.KeyDownHelperMethod);

Thread myThread = new Thread(threadDelegate);

myThread.Start();
Thread.Sleep(50);

class KeyHelperClass
{
public bool externalBoolMagicNumber;
public bool c, d;
KeyEventArgs e;

public KeyHelperClass(KeyEventArgs e)
{
c = d = false;
externalBool = false;
this.e = e;
}
public void KeyDownHelperMethod()
{

if ((e.KeyCode == Keys.C))
{
c = true;
externalBoolMagicNumber = false;

}

if ((e.KeyCode == Keys.D))
{
d = true;
externalBoolMagicNumber = false;
}

if (c & d)
{
externalBoolMagicNumber = true; // magic
number set

}

}

}

} //end of keyhelperclass
 
P

Pavel Minaev

Compound question:  first, and this is not easy, if there's a way to
detect multiple simultaneous key presses in C# let me know (in the
below code, keys c and d being pressed simultaneously or nearly so).
I researched this and for C# (as opposed to MFC) there is no library
function, and no easy way, though some code on the net suggested that
you set up a thread that 'lives' for a certain time, then, if keys are
pressed in that certain time, you set a magic number or bool (global)
to detect this condition.

First of all, there's really no such thing as a "simultaneous
keypress", at least by WinForms definition - some key is always
pressed first. You can do as you were suggested, tracking keydown &
keyup events and measuring time difference between them - if, say two
keydown events occur within 50ms without intervening keyup, then you
could treat it as a simultaneous keypress (but the true sequence of
events is still always linear - "key A down, key B down".) There's
absolutely no need for threads here, either - plain single-threaded
event-driven way is quite enough to handle this (the usual way, by
setting the flags & checking for them later - though in this case,
you'll also need to remember the time of keypresses so that you can
check how much time passed between them).
 
R

raylopez99

Pavel said:
First of all, there's really no such thing as a "simultaneous
keypress", at least by WinForms definition - some key is always
pressed first. You can do as you were suggested, tracking keydown &
keyup events and measuring time difference between them - if, say two
keydown events occur within 50ms without intervening keyup, then you
could treat it as a simultaneous keypress (but the true sequence of
events is still always linear - "key A down, key B down".) There's
absolutely no need for threads here, either - plain single-threaded
event-driven way is quite enough to handle this (the usual way, by
setting the flags & checking for them later - though in this case,
you'll also need to remember the time of keypresses so that you can
check how much time passed between them).

Thanks Pavel. You confirmed there is no easy workaround, and that
there is no need for thread.

Your solution of setting up a timer and then checking to see if the
second key is pressed within XYZ ms of the first key is a good
solution.

Finally, if you care to say, what uses are the 'thread' class for a
single processor system, please let me know. I can't think of any
offhand. Of course in theory we're all supposed to be coding for
multiprocessors and making everything as thread independent as
possible (for the future) but that's another story.

RL
 
B

Barry Kelly

raylopez99 said:
Finally, if you care to say, what uses are the 'thread' class for a
single processor system, please let me know. I can't think of any
offhand.

For concurrency without parallelism.

-- Barry
 

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