CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2003
    Posts
    108

    Trying to catch arrows keydown events

    Hello.

    I am able to do that by overriding "ProcessCmdKey", but I have problem to detect "shift + arrow" or "ctrl + arrow" keydown event (I need also to be able to detect that). So that would be much easier if I could catch the event in the main form keydown event (Form1_KeyDown) !

    I read that setting KeyPreview to true should be the solution. So I added this line: this.KeyPreview = true;
    But I am still not able to catch the arrow keydown event, when the focus is on a button.

    It there a way to catch arrow keydown events into the main form keydown event (Form1_KeyDown) ?

    Or is it an obligation to override "ProcessCmdKey" ?

    If the lonely way is to override "ProcessCmdKey", is it possible to catch also "shift + arrow" and "ctrl + arrow" from there ?

    Thank you.

  2. #2
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    Re: Trying to catch arrows keydown events

    Code:
    const int WM_KEYDOWN = 0x100;
    const int WM_SYSKEYDOWN = 0x104;
    protected override bool ProcessCmdKey(ref Message m, Keys keyCode)
    {
        if ((m.Msg == WM_KEYDOWN) || (m.Msg == WM_SYSKEYDOWN))
        {
            switch (keyCode)
            {
                case Keys.F | Keys.Control:
                    //do something;
                    break;
                case Keys.F5:
                    //do something;
                    break;
                case Keys.Control | Keys.F10:
                    //do something;
                    break;
            }
        }
        return base.ProcessCmdKey(ref m, keyCode);
    }
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

  3. #3
    Join Date
    Apr 2003
    Posts
    108

    Re: Trying to catch arrows keydown events

    This works well.
    Thank you very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured