Advertisement
Guest User

hover-right-click.ahk

a guest
Mar 3rd, 2018
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; This script was adapted from a script graciously provided by
  2. ; Justice Frangipane from Tablet Pro (www.tabletpro.net), to
  3. ; specifically remap the side button on the Surface Pen to the
  4. ; right mouse button while the pen is hovering on the screen.
  5.  
  6. ; In order to run this script, you'll need AutoHotKey and AHKHID.ahk
  7. ; AutoHotKey can be downloaded here:
  8. ; https://autohotkey.com/foundation/
  9.  
  10. ; AHKHID.ahk can be downloaded here:
  11. ; https://github.com/jleb/AHKHID/
  12.  
  13. ; INSTRUCTIONS:
  14. ; Place AHKHID.ahk in the same directory as this script, then run the
  15. ; following:
  16.  
  17. ; AutoHotKey.exe hover-right-click.ahk
  18.  
  19. ; You can also make a batch file that runs this command and add it to your
  20. ; startup folder so that it runs automatically when your PC starts.
  21.  
  22. ; If you find this script useful, consider Tablet Pro as well. It's a cool
  23. ; SW and the guys there (Justice) freely shared their knowledge.
  24.  
  25. ; Disclaimer: this script is provided "as is". The author(s) shall not be
  26. ; liable for any damage caused by the use of this script or its derivatives,
  27. ; etc. I've only done minor testing of this script and it works for me.
  28.  
  29. #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
  30. SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
  31. SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
  32. #Singleinstance force
  33. ; Surface Pro 3 AutoHotkey
  34. ; https://github.com/jonathanyip/Surface-Pro-3-AutoHotkey
  35. ;
  36. ; Update History:
  37. ; Base version, Sep 14, 2016.
  38. ; First Release: Apr 10, 2017
  39. ; Last Updated: Apr. 15, 2017 by Leo Reyes
  40. ; Minor bug fixes regarding the interaction of Ctrl, Alt and Shift keys with
  41. ; hover right-click.
  42.  
  43. ; Set up our pen constants
  44. global PEN_NOT_HOVERING := 0x0 ; Pen is moved away from screen.
  45. global PEN_HOVERING := 0x1 ; Pen is hovering above screen.
  46. global PEN_TOUCHING := 0x3 ; Pen is touching screen.
  47. global PEN_2ND_BTN_HOVERING := 0x5 ; 2nd button is pressed.
  48.  
  49. ; Send Alt, Ctrl and Shift keys along with the right-click if
  50. ; necessary
  51. SendModifierKeys() {
  52.     static lastAlt   := 0
  53.     static lastCtrl  := 0
  54.     static lastShift := 0
  55.     AltState   := GetKeyState("Alt")
  56.     CtrlState  := GetKeyState("Ctrl")
  57.     ShiftState := GetKeyState("Shift")
  58.     if (AltState <> lastAlt) {
  59.         if (AltState)
  60.         {
  61.             Send {Alt Down}
  62.         } else {
  63.             Send {Alt Up}
  64.         }
  65.         lastAlt := AltState
  66.     }
  67.  
  68.     if (CtrlState <> lastCtrl) {
  69.         if (CtrlState) {
  70.             Send {Ctrl Down}
  71.         } else {
  72.             Send {Ctrl Up}
  73.         }
  74.         lastCtrl := CtrlState
  75.     }
  76.  
  77.     if (ShiftState <> lastShift) {
  78.         if (ShiftState) {
  79.             Send {Shift Down}
  80.         } else {
  81.             Send {Shift Up}
  82.         }
  83.         lastShift := ShiftState
  84.     }
  85. }
  86.  
  87. ; This is where we remap the side button to the right-click events.
  88. PenCallback(input, lastInput) {
  89.     if (input = PEN_2ND_BTN_HOVERING And lastInput = PEN_HOVERING) {
  90.     SendModifierKeys() 
  91.     Send {RButton Down}
  92.     }
  93.  
  94.     if (input = PEN_HOVERING And lastInput = PEN_2ND_BTN_HOVERING) {
  95.     SendModifierKeys()
  96.     Send {RButton Up}
  97.     }
  98. }
  99.  
  100. #include AHKHID.ahk
  101.  
  102. ; Set up other constants
  103. ; USAGE_PAGE and USAGE might change on different devices. 13/2 works for
  104. ; the Surface Pen
  105. WM_INPUT := 0xFF
  106. USAGE_PAGE := 13
  107. USAGE := 2
  108.  
  109. ; Set up AHKHID constants
  110. AHKHID_UseConstants()
  111.  
  112. ; Register the pen
  113. AHKHID_AddRegister(1)
  114. AHKHID_AddRegister(USAGE_PAGE, USAGE, A_ScriptHwnd, RIDEV_INPUTSINK)
  115. AHKHID_Register()
  116.  
  117. ; Intercept WM_INPUT
  118. OnMessage(WM_INPUT, "InputMsg")
  119.  
  120. ; Callback for WM_INPUT
  121. ; Isolates the bits responsible for the pen states from the raw data.
  122.  
  123. InputMsg(wParam, lParam) {
  124.     Local type, inputInfo, inputData, raw, proc
  125.     static lastInput := PEN_NOT_HOVERING
  126.    
  127.     Critical
  128.  
  129.     type := AHKHID_GetInputInfo(lParam, II_DEVTYPE)
  130.     if (type = RIM_TYPEHID) {
  131.         inputData := AHKHID_GetInputData(lParam, uData)
  132.        
  133.     raw := NumGet(uData, 0, "UInt")
  134.         proc := (raw >> 8) & 0x1F
  135.  
  136.     if (proc <> lastInput) {
  137.             PenCallback(proc, lastInput)
  138.             lastInput := proc
  139.     }
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement