Run through events and test if they work in C#

82 views Asked by At

I have a program that makes use of global hotkeys (ALTGR + F1 -> F12):

        HotKeyManager.RegisterHotKey(Keys.F1, KeyModifier.AltGr);
        HotKeyManager.RegisterHotKey(Keys.F2, KeyModifier.AltGr);
        HotKeyManager.RegisterHotKey(Keys.F3, KeyModifier.AltGr);
        HotKeyManager.RegisterHotKey(Keys.F4, KeyModifier.AltGr);
        HotKeyManager.RegisterHotKey(Keys.F5, KeyModifier.AltGr);
        HotKeyManager.RegisterHotKey(Keys.F6, KeyModifier.AltGr);
        HotKeyManager.RegisterHotKey(Keys.F7, KeyModifier.AltGr);
        HotKeyManager.RegisterHotKey(Keys.F8, KeyModifier.AltGr);
        HotKeyManager.RegisterHotKey(Keys.F9, KeyModifier.AltGr);
        HotKeyManager.RegisterHotKey(Keys.F10, KeyModifier.AltGr);
        HotKeyManager.RegisterHotKey(Keys.F11, KeyModifier.AltGr);
        HotKeyManager.RegisterHotKey(Keys.F12, KeyModifier.AltGr);
        HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);

I then run through a switch to see which key was pressed and then it needs to run some code, if that hotkey was pressed:

            switch (e.Key)
            {
                case Keys.F1 when e.Modifiers == KeyModifier.AltGr:
                    Console.WriteLine("F1 was hit");
                    _createAndWriteFile($"{_localDestination}\\HotKeyPressedEvent.txt", "F1 was hit");
                    break;
                case Keys.F2 when e.Modifiers == KeyModifier.AltGr:
                    Console.WriteLine("F2 was hit");
                    _createAndWriteFile($"{_localDestination}\\HotKeyPressedEvent.txt", "F2 was hit");
                    break;
                case Keys.F3 when e.Modifiers == KeyModifier.AltGr:
                    Console.WriteLine("F3 was hit");
                    _createAndWriteFile($"{_localDestination}\\HotKeyPressedEvent.txt", "F3 was hit");
                    break;
                case Keys.F4 when e.Modifiers == KeyModifier.AltGr:
                    Console.WriteLine("F4 was hit");
                    _createAndWriteFile($"{_localDestination}\\HotKeyPressedEvent.txt", "F4 was hit");
                    break;
                case Keys.F5 when e.Modifiers == KeyModifier.AltGr:
                    Console.WriteLine("F5 was hit");
                    _createAndWriteFile($"{_localDestination}\\HotKeyPressedEvent.txt", "F5 was hit");
                    break;
                case Keys.F6 when e.Modifiers == KeyModifier.AltGr:
                    Console.WriteLine("F6 was hit");
                    _createAndWriteFile($"{_localDestination}\\HotKeyPressedEvent.txt", "F6 was hit");
                    break;
                case Keys.F7 when e.Modifiers == KeyModifier.AltGr:
                    Console.WriteLine("F7 was hit");
                    _createAndWriteFile($"{_localDestination}\\HotKeyPressedEvent.txt", "F7 was hit");
                    break;
                case Keys.F8 when e.Modifiers == KeyModifier.AltGr:
                    Console.WriteLine("F8 was hit");
                    _createAndWriteFile($"{_localDestination}\\HotKeyPressedEvent.txt", "F8 was hit");
                    break;
                case Keys.F9 when e.Modifiers == KeyModifier.AltGr:
                    Console.WriteLine("F9 was hit");
                    _createAndWriteFile($"{_localDestination}\\HotKeyPressedEvent.txt", "F9 was hit");
                    break;
                case Keys.F10 when e.Modifiers == KeyModifier.AltGr:
                    Console.WriteLine("F10 was hit");
                    _createAndWriteFile($"{_localDestination}\\HotKeyPressedEvent.txt", "F10 was hit");
                    break;
                case Keys.F11 when e.Modifiers == KeyModifier.AltGr:
                    Console.WriteLine("F11 was hit");
                    _createAndWriteFile($"{_localDestination}\\HotKeyPressedEvent.txt", "F11 was hit");
                    break;
                case Keys.F12 when e.Modifiers == KeyModifier.AltGr:
                    Console.WriteLine("F12 was hit");
                    _createAndWriteFile($"{_localDestination}\\HotKeyPressedEvent.txt", "F12 was hit");
                    break;
            }

        }

My problem is, that it seems like not all hotkeys works and it is random when it works or not - for instance, ALTGR + F3 can work when the application is open and then not work when the application has restarted. When debugging in VS, it does run through all hotkeys and registers them in the EventHandler.

How can I test that the hotkeys work at runtime and if not, then make sure that they do?

0

There are 0 answers