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
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