1

In visual studio, I'm using hotkeys they work but if I click off the application they don't work and I need them to

This is my code I'm using

private void Form1_KeyDown(object sender, KeyEventArgs e) {
     if(e.KeyCode == Keys.B) {
            button1.PerformClick();
     }
}

thats all ived tried and i can think of

Scorp
  • 19
  • 1
  • so, the hot keys work when you click off your exe and do not work when you have your exe in focus is that correct? – Simon Price Sep 21 '17 at 07:09
  • @CodeWarrior I am reading that the exact opposite way. Focus on App: ok, Focus off Ap : no. Which of course would be expected behavior. – Fildor Sep 21 '17 at 07:11
  • @Fildor, yes, dyslexia has the better of me there... ive re-read it again and yes... you're right – Simon Price Sep 21 '17 at 07:12
  • 2
    Global hot keys are a bad idea: are you going to prevent the user using the B key? – Richard Sep 21 '17 at 07:14
  • no im not going to prevent them @Richard – Scorp Sep 21 '17 at 07:17
  • how do you do global hotkey – Scorp Sep 21 '17 at 07:35
  • This code does not have any issue, but this will work only if you have focus to Form, if yu have focus to any other element in form, this will not work..... – Shiwanka Chathuranga Sep 21 '17 at 07:44
  • What in my head is keylogger... You can search in google with "keylogger vb.net", some have source code with sample for your reference, but I doubt that is a good idea – Prisoner Sep 21 '17 at 07:44
  • @Scorp If you are looking for OS-Level HotKeys (global), check out this article: https://ourcodeworld.com/articles/read/573/how-to-register-a-single-or-multiple-global-hotkeys-for-a-single-key-in-winforms – Carlos Delgado Sep 21 '17 at 07:48

1 Answers1

0

you have two options in this case:

  1. you can register a hotkey in the registry of windows itself.
  2. you can use the native winapi keyhook, but in that case the antivirus might work against you
Fildor
  • 14,510
  • 4
  • 35
  • 67
CorrupD
  • 36
  • 6