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. Capturing F5 Keypress

Capturing F5 Keypress

Scheduled Pinned Locked Moved C#
helpquestion
5 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.
  • R Offline
    R Offline
    ramzg
    wrote on last edited by
    #1

    Hi, I am trying to enable the keypress event for 'F5' key to refresh my form... Because I found that the normal KeyPress event does not work for function keys, I tried overriding the PreProcessMessage() function, as follows : public override bool PreProcessMessage(ref Message msg) { try { if (msg.Msg == WM_KEYDOWN) { Keys keyCode = (Keys)msg.WParam & Keys.KeyCode; if (keyCode == Keys.F5) { //code } } return PreProcessMessage(ref msg); } catch (Exception ex) { throw; } } But the control never gets into this method... Can anyone help me fix this? Thanks in advance, ramzg

    S 1 2 Replies Last reply
    0
    • R ramzg

      Hi, I am trying to enable the keypress event for 'F5' key to refresh my form... Because I found that the normal KeyPress event does not work for function keys, I tried overriding the PreProcessMessage() function, as follows : public override bool PreProcessMessage(ref Message msg) { try { if (msg.Msg == WM_KEYDOWN) { Keys keyCode = (Keys)msg.WParam & Keys.KeyCode; if (keyCode == Keys.F5) { //code } } return PreProcessMessage(ref msg); } catch (Exception ex) { throw; } } But the control never gets into this method... Can anyone help me fix this? Thanks in advance, ramzg

      S Offline
      S Offline
      S a n d y
      wrote on last edited by
      #2

      You should change the keypreview property to true of form. if it is set to false.

      R 1 Reply Last reply
      0
      • S S a n d y

        You should change the keypreview property to true of form. if it is set to false.

        R Offline
        R Offline
        ramzg
        wrote on last edited by
        #3

        Hi S a n d y, I tried what had posted... But still, the control doesnt get inside the method... Do i have to do something else? Thanks, ramzg

        1 Reply Last reply
        0
        • R ramzg

          Hi, I am trying to enable the keypress event for 'F5' key to refresh my form... Because I found that the normal KeyPress event does not work for function keys, I tried overriding the PreProcessMessage() function, as follows : public override bool PreProcessMessage(ref Message msg) { try { if (msg.Msg == WM_KEYDOWN) { Keys keyCode = (Keys)msg.WParam & Keys.KeyCode; if (keyCode == Keys.F5) { //code } } return PreProcessMessage(ref msg); } catch (Exception ex) { throw; } } But the control never gets into this method... Can anyone help me fix this? Thanks in advance, ramzg

          1 Offline
          1 Offline
          12Code
          wrote on last edited by
          #4

          You use the wrong syntax!, the correct syntax is

          protected override bool ProcessCmdKey(
          ref Message m,
          Keys keyData
          )

          Try this:

              protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
              {     
                  if (keyData == Keys.F5)
                  {
                      MessageBox.Show("F5 pressed!");
                  }          
                  return base.ProcessCmdKey(ref msg, keyData);
          }
          

          Hope this help.

          ;)*12Code

          R 1 Reply Last reply
          0
          • 1 12Code

            You use the wrong syntax!, the correct syntax is

            protected override bool ProcessCmdKey(
            ref Message m,
            Keys keyData
            )

            Try this:

                protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
                {     
                    if (keyData == Keys.F5)
                    {
                        MessageBox.Show("F5 pressed!");
                    }          
                    return base.ProcessCmdKey(ref msg, keyData);
            }
            

            Hope this help.

            ;)*12Code

            R Offline
            R Offline
            ramzg
            wrote on last edited by
            #5

            Hi 12Code, Thanks a lot for the help... It is working now... :-) Thanks, ramzg

            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