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