KeyUp/KeyDown, Playing wav

Dec 25, 2008 at 10:36 PM
Edited Dec 25, 2008 at 10:37 PM

Scott

Thank you for a nice program, my 2-year daughter loves it :)

Occasionally, she press a key and holds it, but nothing happens until the key is released.

I have traced the issue to MainWindow.xaml.cs, method OnKeyUp();
By replacing it with the event OnKeyDown I was able to make response to key pressed almost immediate
but this required another adjustmet to avoid too many duplicates; so I have added delay of 1 second between them.
This behavior is now more similar to most editors.

static
DateTime dtPre = DateTime.MinValue;
protected override void OnKeyDown(KeyEventArgs e) {
    
DateTime dtNow = DateTime.Now; TimeSpan ts = dtNow.Subtract(dtPre);
    
if (ts.TotalSeconds > 1) {
        
base.OnKeyDown(e);
        e.Handled =
true;
        controller.ProcessKey(
this, e);
        dtPre = dtNow;
    }
}


On similar topic, was there a reason to use p/invoke to detect keyboard events (KeyboardHook.cs)
Since you are closer to Windows source, I am guessing there may be some advantage to avoiding managed code?


I have also observed that you are using p/invoke (dllimport) to play wav files (audio.cs)
this is done in a separate thread for each call, and wav files are stored as resourced in exe.

I was using managed code like this for similar tasks:
using (System.Media.SoundPlayer sndplayr = new System.Media.SoundPlayer(sWavFile)) { sndplayr.Play(); }

 

 

Was there some reason to use p/invoke for playing wavs, and to place each in separate thread?

Dragan Sretenovic

 

Sep 20, 2010 at 6:08 PM

Doesn't doing this work better?

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (!e.IsRepeat)
            {
                e.Handled = true;
                controller.ProcessKey(this, e);
            }
        }