Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. About Event Handlig - KeyDown

About Event Handlig - KeyDown

Scheduled Pinned Locked Moved C#
help
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    selcuks
    wrote on last edited by
    #1

    Hello, I have written a method to process shortcut entries in my main form that simply dispatches messages to related methods for the button presses. But my problem is that when Key.Left or Key.Right is pressed for the "first" time, KeyDown event is not triggered for my following method: KeyPreview property of main form is set to true,

    private void shortcutKeyDown(object sender, KeyEventArgs e)
    {

    if (!e.Control && !e.Alt && !e.Shift) // I want to process if Ctrl Alt or Shift Keys are not pressed.
    {
    switch (e.KeyCode)// Examine KeyCode
    {
    case (Keys.Left):
    {
    CallRelatedMethod(this, e);
    break;
    }
    case (Keys.Right):
    {
    CallRelatedMethod(this, e);
    break;
    }
    case (Keys.E):
    {
    CallRelatedMethod(this, e);
    break;
    }
    case (Keys.W):
    {
    CallRelatedMethod(this, e);
    break;
    }
    case (Keys.P):
    {
    CallRelatedMethod(this, e);
    break;
    }
    default:
    break;
    } // switch (e.KeyValue)
    } //!e.Control || !e.Alt || !e.Shift

    } // shortcutKeyDown

    Test Notes: Other keys "W, E, P" triggers the Keydown event without any problems. When Left or Right key is pressed for the second time Keydown event is triggered. Could not find similar problems in google or here so probably I am missing a simple point but I cannot see. Any help is appreciated. Thank you,

    E M 2 Replies Last reply
    0
    • S selcuks

      Hello, I have written a method to process shortcut entries in my main form that simply dispatches messages to related methods for the button presses. But my problem is that when Key.Left or Key.Right is pressed for the "first" time, KeyDown event is not triggered for my following method: KeyPreview property of main form is set to true,

      private void shortcutKeyDown(object sender, KeyEventArgs e)
      {

      if (!e.Control && !e.Alt && !e.Shift) // I want to process if Ctrl Alt or Shift Keys are not pressed.
      {
      switch (e.KeyCode)// Examine KeyCode
      {
      case (Keys.Left):
      {
      CallRelatedMethod(this, e);
      break;
      }
      case (Keys.Right):
      {
      CallRelatedMethod(this, e);
      break;
      }
      case (Keys.E):
      {
      CallRelatedMethod(this, e);
      break;
      }
      case (Keys.W):
      {
      CallRelatedMethod(this, e);
      break;
      }
      case (Keys.P):
      {
      CallRelatedMethod(this, e);
      break;
      }
      default:
      break;
      } // switch (e.KeyValue)
      } //!e.Control || !e.Alt || !e.Shift

      } // shortcutKeyDown

      Test Notes: Other keys "W, E, P" triggers the Keydown event without any problems. When Left or Right key is pressed for the second time Keydown event is triggered. Could not find similar problems in google or here so probably I am missing a simple point but I cannot see. Any help is appreciated. Thank you,

      E Offline
      E Offline
      enginco
      wrote on last edited by
      #2

      Maybe you should write in FormKeyDown event or if you can try to catch windows keypresses.

      1 Reply Last reply
      0
      • S selcuks

        Hello, I have written a method to process shortcut entries in my main form that simply dispatches messages to related methods for the button presses. But my problem is that when Key.Left or Key.Right is pressed for the "first" time, KeyDown event is not triggered for my following method: KeyPreview property of main form is set to true,

        private void shortcutKeyDown(object sender, KeyEventArgs e)
        {

        if (!e.Control && !e.Alt && !e.Shift) // I want to process if Ctrl Alt or Shift Keys are not pressed.
        {
        switch (e.KeyCode)// Examine KeyCode
        {
        case (Keys.Left):
        {
        CallRelatedMethod(this, e);
        break;
        }
        case (Keys.Right):
        {
        CallRelatedMethod(this, e);
        break;
        }
        case (Keys.E):
        {
        CallRelatedMethod(this, e);
        break;
        }
        case (Keys.W):
        {
        CallRelatedMethod(this, e);
        break;
        }
        case (Keys.P):
        {
        CallRelatedMethod(this, e);
        break;
        }
        default:
        break;
        } // switch (e.KeyValue)
        } //!e.Control || !e.Alt || !e.Shift

        } // shortcutKeyDown

        Test Notes: Other keys "W, E, P" triggers the Keydown event without any problems. When Left or Right key is pressed for the second time Keydown event is triggered. Could not find similar problems in google or here so probably I am missing a simple point but I cannot see. Any help is appreciated. Thank you,

        M Offline
        M Offline
        Mbah Dhaim
        wrote on last edited by
        #3

        i modify your code to :

        if (e.Control || e.Alt || e.Shift) return;
        switch (e.KeyCode)// Examine KeyCode
        {
        case (Keys.Left):
        {
        CallRelatedMethod(this, e);
        break;
        }
        case (Keys.Right):
        {
        CallRelatedMethod(this, e);
        break;
        }
        case (Keys.E):
        {
        CallRelatedMethod(this, e);
        break;
        }
        case (Keys.W):
        {
        CallRelatedMethod(this, e);
        break;
        }
        case (Keys.P):
        {
        CallRelatedMethod(this, e);
        break;
        }
        default:
        break;
        } // switch (e.KeyValue)

        hope it help

        dhaim program is hobby that make some money as side effect :)

        S 1 Reply Last reply
        0
        • M Mbah Dhaim

          i modify your code to :

          if (e.Control || e.Alt || e.Shift) return;
          switch (e.KeyCode)// Examine KeyCode
          {
          case (Keys.Left):
          {
          CallRelatedMethod(this, e);
          break;
          }
          case (Keys.Right):
          {
          CallRelatedMethod(this, e);
          break;
          }
          case (Keys.E):
          {
          CallRelatedMethod(this, e);
          break;
          }
          case (Keys.W):
          {
          CallRelatedMethod(this, e);
          break;
          }
          case (Keys.P):
          {
          CallRelatedMethod(this, e);
          break;
          }
          default:
          break;
          } // switch (e.KeyValue)

          hope it help

          dhaim program is hobby that make some money as side effect :)

          S Offline
          S Offline
          selcuks
          wrote on last edited by
          #4

          Thank you for your time guys, With your approach to the method here are some more details: - I have assigned this method to every control that can have focus. (I have only a toolbar and a form thats all.) (Subscribed to form, toolbar keyDown events.) - dhaim, thank you for your modification but unfortunately I dont have a problem as the code does not run in the if() statement. When I debug the code and insert a break to the beginning of the method, I saw that the Keydown event is not triggered when I first press left or right button. Which means the method is never executed although a keydown event occurs. Thank you again, for your precious time.

          E 1 Reply Last reply
          0
          • S selcuks

            Thank you for your time guys, With your approach to the method here are some more details: - I have assigned this method to every control that can have focus. (I have only a toolbar and a form thats all.) (Subscribed to form, toolbar keyDown events.) - dhaim, thank you for your modification but unfortunately I dont have a problem as the code does not run in the if() statement. When I debug the code and insert a break to the beginning of the method, I saw that the Keydown event is not triggered when I first press left or right button. Which means the method is never executed although a keydown event occurs. Thank you again, for your precious time.

            E Offline
            E Offline
            enginco
            wrote on last edited by
            #5

            some controls takes the control when you focused. you must lostfocus before catch the keypress. like webBrowser, datagridview. You must override theirs events..

            S 1 Reply Last reply
            0
            • E enginco

              some controls takes the control when you focused. you must lostfocus before catch the keypress. like webBrowser, datagridview. You must override theirs events..

              S Offline
              S Offline
              selcuks
              wrote on last edited by
              #6

              I have tested your suggestion and I am pretty sure that main form has the focus when the program is first executed and left/right arrow is pressed as a shortcut. Thank you,

              E 1 Reply Last reply
              0
              • S selcuks

                I have tested your suggestion and I am pretty sure that main form has the focus when the program is first executed and left/right arrow is pressed as a shortcut. Thank you,

                E Offline
                E Offline
                enginco
                wrote on last edited by
                #7

                yes sir, I found your problem :) use FormKeyUp event. It works for all. also here is a simple sample.

                private void frmMain_KeyUp(object sender, KeyEventArgs e)
                {
                if (e.KeyValue == 114) // F3
                SendKeys.Send("^{TAB}"); // CTRL + TAB
                if (e.KeyValue == 115) // F4
                SendKeys.Send("^+{TAB}"); // CTRL + SHIFT + TAB
                }

                S 1 Reply Last reply
                0
                • E enginco

                  yes sir, I found your problem :) use FormKeyUp event. It works for all. also here is a simple sample.

                  private void frmMain_KeyUp(object sender, KeyEventArgs e)
                  {
                  if (e.KeyValue == 114) // F3
                  SendKeys.Send("^{TAB}"); // CTRL + TAB
                  if (e.KeyValue == 115) // F4
                  SendKeys.Send("^+{TAB}"); // CTRL + SHIFT + TAB
                  }

                  S Offline
                  S Offline
                  selcuks
                  wrote on last edited by
                  #8

                  Even if I use KeyUp or KeyDown event the result does not change. Issue is still there. When I debug the code and insert a break to the beginning of the method, I saw that the Keydown/KeyUp event is not triggered when I first press left or right button. Which means the method is never executed although a keydown/KeyUp event occurs. I think I am missing something different than catching the pressed key because when I press the left/right twice everything work just fine. Thank you Engin,

                  E 1 Reply Last reply
                  0
                  • S selcuks

                    Even if I use KeyUp or KeyDown event the result does not change. Issue is still there. When I debug the code and insert a break to the beginning of the method, I saw that the Keydown/KeyUp event is not triggered when I first press left or right button. Which means the method is never executed although a keydown/KeyUp event occurs. I think I am missing something different than catching the pressed key because when I press the left/right twice everything work just fine. Thank you Engin,

                    E Offline
                    E Offline
                    enginco
                    wrote on last edited by
                    #9

                    it's working for me, strange event is triggered when I first press left or right and for all buttons. I don't know "why", sorry

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups