KeyDown without focus?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony

    KeyDown without focus?

    I'm building a simple "breakout" style video-game, and I want keyboard
    control of the paddle.

    What I need is for an event handler to respond whenever a key is pressed,
    regardless of what the focus is set to. So far, the best I've been able to
    accomplish is to create a button and have that button handle the KeyDown
    event. I've tried using KeyDown on the form, but it simply doesn't work.

    I did a search on this, and found on site that suggested overriding the
    system "OnKeyDown" event handlers - but I haven't gotten this to work,
    either. It could be because I'm putting it in the wrong place, or perhaps
    it's just not the way to do it.

    I'm using Visual C# Studio - the new Beta edition, if that makes a
    difference. Any suggestions would be greatly appreciated.


  • Morten Wennevik

    #2
    Re: KeyDown without focus?

    Hi Tony,

    Did you set the Form.KeyPreview property to true? The parent form should then be notified of key events, even though a child control has focus.

    To be able to use OnKeyDown you need to inherit from some Control and override the base KeyDown event. You can do that in the parent form

    //Inside Form1
    protected override OnKeyDown(KeyEv entArgs e)
    {
    }

    However, the effect is very much like

    Form1.KeyDown += new KeyEventHandler (Form1_KeyDown) ;
    ....
    private void Form1_KeyDown(o bject sender, KeyEventArgs e)
    {
    }



    On Fri, 27 May 2005 23:30:33 +0200, Tony <someone@somewh ere.not> wrote:
    [color=blue]
    > I'm building a simple "breakout" style video-game, and I want keyboard
    > control of the paddle.
    >
    > What I need is for an event handler to respond whenever a key is pressed,
    > regardless of what the focus is set to. So far, the best I've been able to
    > accomplish is to create a button and have that button handle the KeyDown
    > event. I've tried using KeyDown on the form, but it simply doesn't work.
    >
    > I did a search on this, and found on site that suggested overriding the
    > system "OnKeyDown" event handlers - but I haven't gotten this to work,
    > either. It could be because I'm putting it in the wrong place, or perhaps
    > it's just not the way to do it.
    >
    > I'm using Visual C# Studio - the new Beta edition, if that makes a
    > difference. Any suggestions would be greatly appreciated.
    >
    >
    >[/color]



    --
    Happy coding!
    Morten Wennevik [C# MVP]

    Comment

    • Tony

      #3
      Re: KeyDown without focus?

      "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
      news:op.srgnuzt dklbvpo@stone.. .[color=blue]
      > Hi Tony,
      >
      > Did you set the Form.KeyPreview property to true? The parent form should
      > then be notified of key events, even though a child control has focus.[/color]

      No - none of the samples I found mentioned that at all. Would the default
      for that be either false or null, then?
      [color=blue]
      > To be able to use OnKeyDown you need to inherit from some Control and
      > override the base KeyDown event. You can do that in the parent form
      >
      > //Inside Form1
      > protected override OnKeyDown(KeyEv entArgs e)
      > {
      > }[/color]

      I already had that - but I needed the Form.KeyPreview to be changed.

      That seems to have done the trick for the immediate problem - thanks!


      Now, I have another problem - I can't read the arrow keys, or Enter or Tab.

      Right now, I just have a label showing the text - the event handler is just:

      whichkey.Text = keyArgs.KeyData .ToString();

      (keyArgs is set earlier).

      This gives me "Space" for the space bar, "Escape" for the escape key, etc -
      but nothing for Tab, Enter, or the four arrow keys.

      I'm guessing I'm missing something on those, too, but I have no idea what it
      could be... (in the meantime, I can at least use the numeric keypad for
      direction control now - thanks again!)


      Comment

      • Morten Wennevik

        #4
        Re: KeyDown without focus?

        On Sat, 28 May 2005 05:14:59 +0200, Tony <someone@somewh ere.not> wrote:
        [color=blue]
        > "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
        > news:op.srgnuzt dklbvpo@stone.. .[color=green]
        >> Hi Tony,
        >>
        >> Did you set the Form.KeyPreview property to true? The parent form should
        >> then be notified of key events, even though a child control has focus.[/color]
        >
        > No - none of the samples I found mentioned that at all. Would the default
        > for that be either false or null, then?
        >[/color]

        The default would be false.
        [color=blue][color=green]
        >> To be able to use OnKeyDown you need to inherit from some Control and
        >> override the base KeyDown event. You can do that in the parent form
        >>
        >> //Inside Form1
        >> protected override OnKeyDown(KeyEv entArgs e)
        >> {
        >> }[/color]
        >
        > I already had that - but I needed the Form.KeyPreview to be changed.
        >
        > That seems to have done the trick for the immediate problem - thanks!
        >
        >
        > Now, I have another problem - I can't read the arrow keys, or Enter or Tab.
        >
        > Right now, I just have a label showing the text - the event handler is just:
        >
        > whichkey.Text = keyArgs.KeyData .ToString();
        >
        > (keyArgs is set earlier).
        >
        > This gives me "Space" for the space bar, "Escape" for the escape key, etc -
        > but nothing for Tab, Enter, or the four arrow keys.
        >
        > I'm guessing I'm missing something on those, too, but I have no idea what it
        > could be... (in the meantime, I can at least use the numeric keypad for
        > direction control now - thanks again!)
        >
        >
        >[/color]

        Hm, for me it traps all keys except TAB.
        However, there is also ProcessCmdKey

        protected override bool ProcessCmdKey(r ef Message msg, Keys keyData)
        {
        return base.ProcessCmd Key (ref msg, keyData);
        }

        This gave me TAB as well.

        --
        Happy coding!
        Morten Wennevik [C# MVP]

        Comment

        • Tony

          #5
          Re: KeyDown without focus?

          "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
          news:op.srgzc7a pklbvpo@stone.. .[color=blue][color=green]
          >>[/color]
          >
          > Hm, for me it traps all keys except TAB.
          > However, there is also ProcessCmdKey
          >
          > protected override bool ProcessCmdKey(r ef Message msg, Keys keyData)
          > {
          > return base.ProcessCmd Key (ref msg, keyData);
          > }
          >
          > This gave me TAB as well.
          >[/color]

          Thanks again - that did everything I needed!


          Comment

          Working...