traping all combinations of keypress in overriding ProcessCmdKey

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

    traping all combinations of keypress in overriding ProcessCmdKey



    Hello,

    i am lookinf for the best way to trap any alphanumeric keypress in all
    multi key combminations and execute some code

    For example , i have a form visible using the form.showdialog method,
    then the user can press keys A, D, E, F, T, and C to peform a
    function.
    What i want is that if a user presses A, or a, or SHIFT A or CNTRL A
    that it executes the same code, BUT NOT IF ALT A is pressed. The same
    applies for the other letters.

    I am overriding the processcmdkey function using the code bellow, and
    it works, but not for ALL the multi key combo's i present above.

    Can anyone help me on how to trap all the key combos i want listed
    above

    thanks for any help

    Peted

    #region ProcessCmdKey
    protected override bool ProcessCmdKey(r ef Message msg, Keys
    keyData)
    {
    const int WM_KEYDOWN = 0x100;
    const int WM_SYSKEYDOWN = 0x104;

    if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
    {
    switch (keyData)
    {
    case Keys.Tab:
    //Do something
    return true;
    break;
    case Keys.F3:
    //Do something
    return true;
    break;

    case Keys.A:
    //Do something
    return true;


    case Keys.D:
    //Do something
    return true;

    case Keys.E:
    //Do something
    return true;

    case Keys.F:
    //Do something
    return true;

    case Keys.T:
    //Do something
    return true;


    case Keys.C:
    //Do something
    return true;

    }
    }
    return base.ProcessCmd Key(ref msg, keyData);
    }
  • Peter Duniho

    #2
    Re: traping all combinations of keypress in overriding ProcessCmdKey

    On Sun, 06 Apr 2008 09:40:16 -0700, <Petedwrote:
    i am lookinf for the best way to trap any alphanumeric keypress in all
    multi key combminations and execute some code
    >
    For example , i have a form visible using the form.showdialog method,
    then the user can press keys A, D, E, F, T, and C to peform a
    function.
    What i want is that if a user presses A, or a, or SHIFT A or CNTRL A
    that it executes the same code, BUT NOT IF ALT A is pressed. The same
    applies for the other letters.
    >
    I am overriding the processcmdkey function using the code bellow, and
    it works, but not for ALL the multi key combo's i present above.
    Why not? What happens? What did you expect to happen instead?

    Pete

    Comment

    • Peted

      #3
      Re: traping all combinations of keypress in overriding ProcessCmdKey


      >I am overriding the processcmdkey function using the code bellow, and
      >it works, but not for ALL the multi key combo's i present above.
      >
      >Why not? What happens? What did you expect to happen instead?

      Pressing A, D, E, F, T, and C keys on thier own works fine

      Capslock and A, D, E, F, T, and C works fine

      Shift and A, D, E, F, T, and C does not work

      Control and A, D, E, F, T, and C does not work

      Alt and A, D, E, F, T, and C does not work
      (I have now decided i do want the alt key combo to also work)

      So any idea how i can get mod the code to make it pick up all those
      modifier and key press combo in addition to just pressing the relevant
      letter ?

      I want all those combos to execute the same code as the single
      keypress

      Any advice appreciated

      thanks

      Peted

      Comment

      • Steve Gerrard

        #4
        Re: traping all combinations of keypress in overriding ProcessCmdKey

        Peted wrote:
        >
        Control and A, D, E, F, T, and C does not work
        To capture Ctrl A separately, you would need
        switch (keyData)
        {
        case (Keys.A | Keys.Control):

        Keys combines the keys being pressed.




        Comment

        • Peter Duniho

          #5
          Re: traping all combinations of keypress in overriding ProcessCmdKey

          On Sun, 06 Apr 2008 18:39:37 -0700, <Petedwrote:
          [...]
          So any idea how i can get mod the code to make it pick up all those
          modifier and key press combo in addition to just pressing the relevant
          letter ?
          Instead of "switch (keyData)" use "switch (keyData & Keys.KeyCode)". That
          will filter out the modifier keys, leaving only the actual key pressed.

          If you decide later that you want some modifier keys but not others, there
          are a variety of ways you can check for that, but they all involve similar
          techniques. That is, using the various mask constants in the Keys
          enumeration to check specifically for modifiers.

          Pete

          Comment

          • Peted

            #6
            Re: traping all combinations of keypress in overriding ProcessCmdKey


            Great, thanks heaps dude

            Peted

            >On Sun, 06 Apr 2008 18:39:37 -0700, <Petedwrote:
            >
            >[...]
            >So any idea how i can get mod the code to make it pick up all those
            >modifier and key press combo in addition to just pressing the relevant
            >letter ?
            >
            >Instead of "switch (keyData)" use "switch (keyData & Keys.KeyCode)". That
            >will filter out the modifier keys, leaving only the actual key pressed.
            >
            >If you decide later that you want some modifier keys but not others, there
            >are a variety of ways you can check for that, but they all involve similar
            >techniques. That is, using the various mask constants in the Keys
            >enumeration to check specifically for modifiers.
            >
            >Pete

            Comment

            Working...