Click here to Skip to main content
16,004,919 members
Home / Discussions / C#
   

C#

 
GeneralRe: Again, Two Keys At Once Pin
MasterSharp19-Nov-07 13:08
MasterSharp19-Nov-07 13:08 
GeneralRe: Again, Two Keys At Once Pin
Skippums19-Nov-07 13:15
Skippums19-Nov-07 13:15 
GeneralRe: Again, Two Keys At Once Pin
MasterSharp19-Nov-07 13:59
MasterSharp19-Nov-07 13:59 
GeneralRe: Again, Two Keys At Once Pin
Skippums19-Nov-07 13:12
Skippums19-Nov-07 13:12 
GeneralRe: Again, Two Keys At Once Pin
Luc Pattyn19-Nov-07 14:00
sitebuilderLuc Pattyn19-Nov-07 14:00 
GeneralRe: Again, Two Keys At Once Pin
MasterSharp19-Nov-07 14:26
MasterSharp19-Nov-07 14:26 
GeneralRe: Again, Two Keys At Once Pin
MasterSharp19-Nov-07 14:45
MasterSharp19-Nov-07 14:45 
GeneralRe: Again, Two Keys At Once Pin
Luc Pattyn19-Nov-07 15:38
sitebuilderLuc Pattyn19-Nov-07 15:38 
I would do something along these lines:

- use a Windows.Forms.Timer, it ticks on the GUI thread
- no lock is needed, all key/timer actions run on GUI thread, without
interrupting each other.
- timer ticks all the time, at 10 times the autorepeat frequency (overclocking)
- each key is synchronized to the overclocked timer. with the const values shown
it would start repeating after 900 to 1000 msec.
- you can easily choose different start repeat and repeat times...

Dictionary<Keys,int>m_keys = new Dictionary<Keys,int>();
Windows.Forms.Timer m_timer = new Windows.Forms.Timer();
const int initialWait = 10;
const int repeatWait = 10;
const int overPeriod = 100; // msec
 
public Constructor() {
    m_timer.Interval=initialWait;
    m_timer.Start();
    m_timer.Tick += new EventHandler(autoRepeatTick);
}
 
private void m_KeyDown(object sender, KeyEventArgs e) {
    Keys key=e.KeyCode;
    handleKey(key);
    if (keys.ContainsKey(key)) {
        m_keys[key]=overClocking;
    } else {
        m_keys.Add(key, overClocking);
    }
}
 
private void m_KeyUp(object sender, KeyEventArgs e) {
    keys.Remove(e.KeyCode);
}
 
private void autoRepeatTick(object sender, ElapsedEventArgs e) {
    // you may add code here to disable autorepeat while form not active!
    foreach (Keys key in m_keys.Keys) {
        int wait=m_keys[key]-1;
 	if (wait<=0) {
	        handleKey(key);
		wait=repeatWait;
	}
	m_keys[key]=wait;
    }
}
 
private void handleKey(Keys key) {
    ... game code
}


Above code is indicative, but has not been tested!

Smile | :)


Luc Pattyn [Forum Guidelines] [My Articles]


this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets


GeneralRe: Again, Two Keys At Once Pin
Skippums19-Nov-07 16:33
Skippums19-Nov-07 16:33 
GeneralRe: Again, Two Keys At Once Pin
Luc Pattyn19-Nov-07 17:14
sitebuilderLuc Pattyn19-Nov-07 17:14 
AnswerRe: Again, Two Keys At Once Pin
Christian Graus19-Nov-07 16:50
protectorChristian Graus19-Nov-07 16:50 
AnswerRe: Again, Two Keys At Once Pin
DaveX8619-Nov-07 18:46
DaveX8619-Nov-07 18:46 
QuestionSample code for setting ReportViewer1.BorderStyle Pin
Davood Riazi19-Nov-07 12:04
Davood Riazi19-Nov-07 12:04 
QuestionOriginal Pin
MasterSharp19-Nov-07 10:42
MasterSharp19-Nov-07 10:42 
AnswerRe: Original Pin
Not Active19-Nov-07 11:39
mentorNot Active19-Nov-07 11:39 
GeneralRe: Original Pin
MasterSharp19-Nov-07 12:18
MasterSharp19-Nov-07 12:18 
AnswerRe: Original Pin
Christian Graus19-Nov-07 12:27
protectorChristian Graus19-Nov-07 12:27 
GeneralRe: Original Pin
MasterSharp19-Nov-07 12:30
MasterSharp19-Nov-07 12:30 
AnswerRe: Original Pin
Colin Angus Mackay19-Nov-07 12:30
Colin Angus Mackay19-Nov-07 12:30 
GeneralRe: Original Pin
MasterSharp19-Nov-07 12:48
MasterSharp19-Nov-07 12:48 
AnswerRe: Original Pin
Expert Coming19-Nov-07 14:25
Expert Coming19-Nov-07 14:25 
QuestionDecleration Problem Pin
ytubis19-Nov-07 10:32
ytubis19-Nov-07 10:32 
AnswerRe: Decleration Problem Pin
Skippums19-Nov-07 10:36
Skippums19-Nov-07 10:36 
QuestionMath Question Pin
Sautin.net19-Nov-07 10:13
Sautin.net19-Nov-07 10:13 
AnswerRe: Math Question Pin
Skippums19-Nov-07 10:32
Skippums19-Nov-07 10:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.