473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

How to detect multiple simultaneous key presses? And a threadquestion

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

Aug 16 '08 #1
4 6434
On Aug 16, 9:38*pm, raylopez99 <raylope...@yahoo.comwrote:
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).
Aug 16 '08 #2


Pavel Minaev wrote:
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
Aug 16 '08 #3
raylopez99 wrote:
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

--
http://barrkel.blogspot.com/
Aug 16 '08 #4
raylopez99 wrote:
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 a concrete example, BTW, see:

http://blogs.msdn.com/oldnewthing/ar...8/8781423.aspx

-- Barry

--
http://barrkel.blogspot.com/
Aug 16 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Claudio Grondi | last post by:
I would like to save time copying the same file (>6 GByte) to various different target storage media connected to the system via USB. Is there a (Python or other) tool able to help me to do...
11
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
6
by: Quiet Man | last post by:
Hi all, I'm designing a fairly simple service that will run on W2K/SP4 and W2K3 servers. It's job is to be a very specialized database server that listens on a given IP address / TCP port and...
4
by: Mike L | last post by:
Currently, when the user presses a letter key the combo box will go to the first item in the collection of the combo box starting with that letter. For example user presses on "R", and "R2F"...
2
by: Perry van Kuppeveld | last post by:
Hi group, Is it possible (and how) to play multiple wav files simultaneous? Thanks Perry
2
by: Chad McCune | last post by:
I'm developing a portal for our intranet where multiple pages are being shown inside several iframes on a single page. The problem i've ran into, is that it seems as if IIS6/ASP.Net serializes...
3
by: Tony | last post by:
I've done a fair bit of searching on this, but I want to be certain about what I've found - so here goes with a long example and three questions: For clarity, let me give an example (a number of...
17
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);"...
5
by: Chamnap | last post by:
Hello, I want to implement multiple uploads with multiple iframe posting to the server at the same time. Do all of you think it is possible to do this? Does this ways has any drawbacks? Give me...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.