gmamaladze / globalmousekeyhook

This library allows you to tap keyboard and mouse, detect and record their activity even when an application is inactive and runs in background.
MIT License
1.04k stars 257 forks source link

how to check for multi key (ex: Ctrl+x) #160

Closed raviruia522 closed 2 years ago

raviruia522 commented 2 years ago

i want to check for multi key (ex: Ctrl+x) how to do it. please help me!

Meetsch commented 2 years ago

The first content of the readme points to the key combination feature documentation: https://github.com/gmamaladze/globalmousekeyhook/blob/master/keycomb.md

The sample code also provides how-to: https://github.com/gmamaladze/globalmousekeyhook/blob/master/examples/ConsoleHook/DetectCombinations.cs

And here are some extracts of my usage:

  //Listen to keyboard bindings
  Hook.GlobalEvents().KeyDown += onKeyDown;

  //the event handler uses a KeyEventArgs which has the list of keys pressed in its KeyData property
  // https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.keyeventargs.keydata?view=windowsdesktop-6.0
  private void onKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  {
        if(e.KeyData == StringToKeys("Ctrl+x"))
        {
              //do some stuff
        }
  }

  //my helper function to create a System.Windows.Forms.Keys from a string representation using the Combination class provided by GlobalMouseKeyHook
  public static System.Windows.Forms.Keys StringToKeys(string keyStr)
  {
      if (String.IsNullOrWhiteSpace(keyStr))
                  throw new ArgumentException("Cannot be null or whitespaces.", nameof(keyStr));
      Combination combination = Combination.FromString(keyStr);
      Keys result = combination.TriggerKey;
      foreach (var chord in combination.Chord)
      {
         result |= chord;
      }
      return result;
  }

Hope it helps

raviruia522 commented 2 years ago

thank you very much!