KeyboardHandler.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using CefSharp;
  5. namespace prBrowser
  6. {
  7. internal class KeyboardHandler : IKeyboardHandler
  8. {
  9. private MainForm myForm;
  10. public static List<SharpHotKey> Hotkeys = new List<SharpHotKey>();
  11. public static void AddHotKey(Form form, Action function, Keys key, bool ctrl = false, bool shift = false, bool alt = false)
  12. {
  13. Utils.AddHotKey(form, function, key, ctrl, shift, alt);
  14. Hotkeys.Add(new SharpHotKey(function, key, ctrl, shift, alt));
  15. }
  16. public KeyboardHandler(MainForm form)
  17. {
  18. myForm = form;
  19. }
  20. public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)
  21. {
  22. return false;
  23. }
  24. public bool OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
  25. {
  26. if (type == KeyType.RawKeyDown)
  27. {
  28. bool ctrlDown = ((int)modifiers).IsBitmaskOn(4);
  29. bool shiftDown = ((int)modifiers).IsBitmaskOn(2);
  30. bool altDown = ((int)modifiers).IsBitmaskOn(8);
  31. foreach (SharpHotKey key in Hotkeys)
  32. {
  33. if (key.KeyCode == windowsKeyCode && key.Ctrl == ctrlDown && key.Shift == shiftDown && key.Alt == altDown)
  34. {
  35. myForm.InvokeOnParent(delegate
  36. {
  37. key.Callback();
  38. });
  39. }
  40. }
  41. }
  42. return false;
  43. }
  44. }
  45. }