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. Setting A Global Hot Key On A Second Form

Setting A Global Hot Key On A Second Form

Scheduled Pinned Locked Moved C#
csharpvisual-studiohelp
4 Posts 2 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
    rfresh
    wrote on last edited by
    #1

    I created a WinForm app in C# using VS 2013 Express. I added code to create a Global Hot Key on the main form. This works fine. My hot key is Ctrl-T. I can press the hot key and make the main form show and hide. Then I created a second form (ChecklistForm) and now I want to press ctrl-T and make that form show and hide. I do not need the main form to do this any more. I just used the main form to test my Global Hot Key code. So I'm having trouble getting the second form to respond to the hot key. When I put a break on the WndProc(), there is no break. Thanks for any help. re lang="c#"> public partial class MainForm : Form { [DllImport("user32.dll")] public static extern IntPtr FindWindow(String sClassName, String AppName); private IntPtr thisWindow; private GlobalHotKeys hotkey; public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { ChecklistForm frm = new ChecklistForm(); frm.Show(); thisWindow = FindWindow(null, "ChecklistForm"); //thisWindow = FindWindow(null, "MainForm"); hotkey = new GlobalHotKeys(thisWindow); hotkey.RegisterHotKeys(); } private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { thisWindow = FindWindow(null, "ChecklistForm"); //thisWindow = FindWindow(null, "MainForm"); hotkey = new GlobalHotKeys(thisWindow); hotkey.UnRegisterHotKeys(); } protected override void WndProc(ref Message keyPressed) { if (keyPressed.Msg == 0x0312) { thisWindow = FindWindow(null, "ChecklistForm"); //thisWindow = FindWindow(null, "MainForm"); IntPtr i = keyPressed.WParam; // not being used ShowChecklist ShowChkList = new ShowChecklist(thisWindow); ShowChkList.execute(); } base.WndProc(ref keyPressed); } } class GlobalHotKeys // CLASS FILE ********************* { public enum fsModifiers { Alt = 0x0001, Control = 0x0002, Shift = 0x0004, Window = 0x0008 } private IntPtr _hWnd; public GlobalHotKeys(IntPtr hWnd) { this._hWnd = hWnd; } public void Registe

    L 1 Reply Last reply
    0
    • R rfresh

      I created a WinForm app in C# using VS 2013 Express. I added code to create a Global Hot Key on the main form. This works fine. My hot key is Ctrl-T. I can press the hot key and make the main form show and hide. Then I created a second form (ChecklistForm) and now I want to press ctrl-T and make that form show and hide. I do not need the main form to do this any more. I just used the main form to test my Global Hot Key code. So I'm having trouble getting the second form to respond to the hot key. When I put a break on the WndProc(), there is no break. Thanks for any help. re lang="c#"> public partial class MainForm : Form { [DllImport("user32.dll")] public static extern IntPtr FindWindow(String sClassName, String AppName); private IntPtr thisWindow; private GlobalHotKeys hotkey; public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { ChecklistForm frm = new ChecklistForm(); frm.Show(); thisWindow = FindWindow(null, "ChecklistForm"); //thisWindow = FindWindow(null, "MainForm"); hotkey = new GlobalHotKeys(thisWindow); hotkey.RegisterHotKeys(); } private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { thisWindow = FindWindow(null, "ChecklistForm"); //thisWindow = FindWindow(null, "MainForm"); hotkey = new GlobalHotKeys(thisWindow); hotkey.UnRegisterHotKeys(); } protected override void WndProc(ref Message keyPressed) { if (keyPressed.Msg == 0x0312) { thisWindow = FindWindow(null, "ChecklistForm"); //thisWindow = FindWindow(null, "MainForm"); IntPtr i = keyPressed.WParam; // not being used ShowChecklist ShowChkList = new ShowChecklist(thisWindow); ShowChkList.execute(); } base.WndProc(ref keyPressed); } } class GlobalHotKeys // CLASS FILE ********************* { public enum fsModifiers { Alt = 0x0001, Control = 0x0002, Shift = 0x0004, Window = 0x0008 } private IntPtr _hWnd; public GlobalHotKeys(IntPtr hWnd) { this._hWnd = hWnd; } public void Registe

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You should not need set a hotkey for each form anew; if the hotkey is used to open your app quickly, then I'd recommend setting a global hotkey. Easiest way is to assign a shortcut to the app[^] to start it, check if it is running, and if yes, bring the other instance to the front.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      R 1 Reply Last reply
      0
      • L Lost User

        You should not need set a hotkey for each form anew; if the hotkey is used to open your app quickly, then I'd recommend setting a global hotkey. Easiest way is to assign a shortcut to the app[^] to start it, check if it is running, and if yes, bring the other instance to the front.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

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

        I don't want a global hot key to START my forms. I stated: "I do not need the main form to do this any more. I just used the main form to test my Global Hot Key code." I want to show and hide my second form with a global hot key and my code above isn't working to do that. Thank you...

        L 1 Reply Last reply
        0
        • R rfresh

          I don't want a global hot key to START my forms. I stated: "I do not need the main form to do this any more. I just used the main form to test my Global Hot Key code." I want to show and hide my second form with a global hot key and my code above isn't working to do that. Thank you...

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          rfresh wrote:

          I don't want a global hot key to START my forms.

          I know; you want to show "a" form from your project when the user presses a specific key.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          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