473,549 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ob ject sender,
KeyEventArgs e) event handler //is this the right place?

//

ThreadStart threadDelegate = new
ThreadStart(myK eyHelperClass.K eyDownHelperMet hod);

Thread myThread = new Thread(threadDe legate);

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

class KeyHelperClass
{
public bool externalBoolMag icNumber;
public bool c, d;
KeyEventArgs e;

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

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

}

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

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

}

}

}

} //end of keyhelperclass

Aug 16 '08 #1
4 6455
On Aug 16, 9:38*pm, raylopez99 <raylope...@yah oo.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 "simultaneo us
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 "simultaneo us
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
4642
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 this, so that I don't need to copy the source file first to the first media, then to the second, etc.? Claudio
11
4912
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., http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=Tidy7IDbDHA.2108%40cpmsftngxa06.phx.gbl) that indicate that ASP will queue up requests when they come...
6
4423
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 handles multiple connections. Client programs will make a connection and pass text strings to the service, which will then return a value for each of...
4
2339
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" appears in the combo box, then the user presses on "H", "HIF" appears. I want the user to be able to enter 2 or 3 characters and the combo box will...
2
1038
by: Perry van Kuppeveld | last post by:
Hi group, Is it possible (and how) to play multiple wav files simultaneous? Thanks Perry
2
2611
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 the requests by the user and only processes one *nugget* (iframe) at a time. It handles multiple requests by different users at the same time just...
3
2883
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 the pages I found had some ambiguity). Say I have a page with 2 buttons ( A and B, for simplicity) and each button will make a different "ajax"...
17
11847
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);" onclick="show('ajaxrequest.php?action=removefield','div1');show('ajaxrequest.php?action=reloaddiv2','div2')">verwijderen</a> While both seperate actions work they dont when I put them together. Anyone know how to fix this ? My ajax.js with funcition...
5
4479
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 some idea, please. Thanks Chamnap
0
7521
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7959
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7473
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
764
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.