deseven

Hotkey selector prototype

Apr 6th, 2017
673
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. EnableExplicit
  2.  
  3. Dim keys.s($FF)
  4. keys($00) = "A"
  5. keys($01) = "S"
  6. keys($02) = "D"
  7. keys($03) = "F"
  8. keys($04) = "H"
  9. keys($05) = "G"
  10. keys($06) = "Z"
  11. keys($07) = "X"
  12. keys($08) = "C"
  13. keys($09) = "V"
  14. keys($0B) = "B"
  15. keys($0C) = "Q"
  16. keys($0D) = "W"
  17. keys($0E) = "E"
  18. keys($0F) = "R"
  19. keys($10) = "Y"
  20. keys($11) = "T"
  21. keys($12) = "1"
  22. keys($13) = "2"
  23. keys($14) = "3"
  24. keys($15) = "4"
  25. keys($16) = "6"
  26. keys($17) = "5"
  27. keys($18) = "="
  28. keys($19) = "9"
  29. keys($1A) = "7"
  30. keys($1B) = "-"
  31. keys($1C) = "8"
  32. keys($1D) = "0"
  33. keys($1E) = "]"
  34. keys($1F) = "O"
  35. keys($20) = "U"
  36. keys($21) = "["
  37. keys($22) = "I"
  38. keys($23) = "P"
  39. keys($25) = "L"
  40. keys($26) = "J"
  41. keys($27) = "'"
  42. keys($28) = "K"
  43. keys($29) = ";"
  44. keys($2A) = ""
  45. keys($2B) = ","
  46. keys($2C) = "/"
  47. keys($2D) = "N"
  48. keys($2E) = "M"
  49. keys($2F) = "."
  50. keys($32) = "`"
  51. keys($24) = "↩"
  52. keys($30) = "Tab"
  53. keys($31) = "Space"
  54. keys($35) = "⎋"
  55. keys($39) = "CAPS"
  56. keys($7A) = "F1"
  57. keys($78) = "F2"
  58. keys($63) = "F3"
  59. keys($76) = "F4"
  60. keys($60) = "F5"
  61. keys($61) = "F6"
  62. keys($62) = "F7"
  63. keys($64) = "F8"
  64. keys($65) = "F9"
  65. keys($6D) = "F10"
  66. keys($67) = "F11"
  67. keys($6F) = "F12"
  68. keys($69) = "F13"
  69. keys($6B) = "F14"
  70. keys($71) = "F15"
  71. keys($6A) = "F16"
  72. keys($40) = "F17"
  73. keys($4F) = "F18"
  74. keys($50) = "F19"
  75. keys($5A) = "F20"
  76. keys($73) = "Home"
  77. keys($77) = "End"
  78. keys($74) = "PgUp"
  79. keys($79) = "PgDown"
  80. keys($0A) = "§"
  81. keys($33) = "Del"
  82.  
  83. ImportC ""
  84.   sel_registerName(str.p-ascii)
  85.   class_addMethod(class, selector, imp, types.p-ascii)
  86. EndImport
  87.  
  88. Define app.i = CocoaMessage(0,0,"NSApplication sharedApplication")
  89. Define placeholder.s = "press keys"
  90. Define activeSelector.i = -1
  91.  
  92. #pressInvite = "press to set"
  93. #enterInvite = "enter keys"
  94.  
  95. OpenWindow(0,#PB_Ignore,#PB_Ignore,150,175,"Hotkey selector prototype",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  96.  
  97. ButtonGadget(0,25,25,100,30,#pressInvite)
  98. ButtonGadget(1,25,75,100,30,#pressInvite)
  99. ButtonGadget(2,25,125,100,30,#pressInvite)
  100. CocoaMessage(0,GadgetID(0),"setBezelStyle:",10)
  101. CocoaMessage(0,GadgetID(1),"setBezelStyle:",10)
  102. CocoaMessage(0,GadgetID(2),"setBezelStyle:",10)
  103.  
  104. ButtonGadget(3,105,25,16,30,"✖")
  105. CocoaMessage(0,GadgetID(3),"setButtonType:",2)
  106. CocoaMessage(0,GadgetID(3),"setBezelStyle:",15)
  107. CocoaMessage(0,GadgetID(3),"setBordered:",0)
  108.  
  109. ButtonGadget(4,105,75,16,30,"✖")
  110. CocoaMessage(0,GadgetID(4),"setButtonType:",2)
  111. CocoaMessage(0,GadgetID(4),"setBezelStyle:",15)
  112. CocoaMessage(0,GadgetID(4),"setBordered:",0)
  113.  
  114. ButtonGadget(5,105,125,16,30,"✖")
  115. CocoaMessage(0,GadgetID(5),"setButtonType:",2)
  116. CocoaMessage(0,GadgetID(5),"setBezelStyle:",15)
  117. CocoaMessage(0,GadgetID(5),"setBordered:",0)
  118.  
  119. HideGadget(3,#True)
  120. HideGadget(4,#True)
  121. HideGadget(5,#True)
  122.  
  123. Define previousHotkey.s = ""
  124.  
  125. Macro activateSelector(gadget)
  126.   If activeSelector <> -1
  127.     deactivateSelector()
  128.   EndIf
  129.   activeSelector = gadget
  130.   Select activeSelector
  131.     Case 0
  132.       HideGadget(3,#True)
  133.     Case 1
  134.       HideGadget(4,#True)
  135.     Case 2
  136.       HideGadget(5,#True)
  137.   EndSelect
  138.   If GetGadgetText(activeSelector) <> #pressInvite
  139.     previousHotkey = GetGadgetText(activeSelector)
  140.   EndIf
  141.   CocoaMessage(0,GadgetID(activeSelector),"highlight:",1)
  142.   SetGadgetText(activeSelector,#enterInvite)
  143. EndMacro
  144.  
  145. Macro deactivateSelector(hotkey = "")
  146.   If activeSelector <> -1
  147.     CocoaMessage(0,GadgetID(activeSelector),"highlight:",0)
  148.     If Len(hotkey)
  149.       SetGadgetText(activeSelector,hotkey)
  150.     ElseIf Len(previousHotkey)
  151.       SetGadgetText(activeSelector,previousHotkey)
  152.     Else
  153.       SetGadgetText(activeSelector,#pressInvite)
  154.     EndIf
  155.     If GetGadgetText(activeSelector) <> #pressInvite
  156.       Select activeSelector
  157.         Case 0
  158.           HideGadget(3,#False)
  159.         Case 1
  160.           HideGadget(4,#False)
  161.         Case 2
  162.           HideGadget(5,#False)
  163.       EndSelect
  164.     EndIf
  165.     activeSelector = -1
  166.     previousHotkey = ""
  167.   EndIf
  168. EndMacro
  169.  
  170. ProcedureC PerformKeyEquivalent(Sender, sel, event)
  171.  
  172.   Shared activeSelector
  173.   Shared previousHotkey
  174.   Shared keys()
  175.   Protected result = #YES  
  176.   Protected currentHtk.s
  177.   Static currentMod.s
  178.  
  179.   If event And activeSelector <> -1
  180.     Select CocoaMessage(0,event,"type")
  181.       Case #NSKeyDown
  182.         Define keyCode = CocoaMessage(0,event,"keyCode")
  183.         If keyCode <= $FF
  184.           If Len(keys(keyCode))
  185.             currentHtk + keys(keyCode)
  186.           EndIf
  187.         EndIf
  188.         ;Debug "KeyDown : keyCode = " + CocoaMessage(0, currentEvent, "keyCode")
  189.         ;Define Long = CocoaMessage(0, currentEvent, "characters")
  190.         ;CocoaMessage(@Long, Long, "UTF8String")
  191.         ;Debug "KeyDown : keyChar = " + Asc(PeekS(Long, -1, #PB_UTF8))
  192.        
  193.       Case #NSKeyUp
  194.         ;Debug "KeyUp : keyCode = " + CocoaMessage(0, currentEvent, "keyCode")
  195.        
  196.       Case #NSFlagsChanged ; SHIFT, CMD, ALT, CTRL, CAPS
  197.         currentMod = ""
  198.         Protected modifierFlags = CocoaMessage(0, event, "modifierFlags")
  199.         If modifierFlags & #NSShiftKeyMask     : currentMod + "⇧" : EndIf
  200.         If modifierFlags & #NSControlKeyMask   : currentMod + "⌃" : EndIf
  201.         If modifierFlags & #NSAlternateKeyMask : currentMod + "⌥" : EndIf
  202.         If modifierFlags & #NSCommandKeyMask   : currentMod + "⌘" : EndIf
  203.        
  204.     EndSelect
  205.     ;Debug currentMod + currentHtk
  206.     If activeSelector <> -1
  207.       If Len(currentMod) = 0 And currentHtk = "⎋"
  208.         deactivateSelector()
  209.       ElseIf Len(currentMod) And Len(currentHtk)
  210.         deactivateSelector(currentMod + currentHtk)
  211.         currentMod = ""
  212.       ElseIf Len(currentMod) Or Len(currentHtk)
  213.         SetGadgetText(activeSelector,currentMod + currentHtk)
  214.       Else
  215.         SetGadgetText(activeSelector,#enterInvite)
  216.       EndIf
  217.     EndIf
  218.   Else
  219.     result = #NO
  220.   EndIf
  221.  
  222.   ProcedureReturn result
  223. EndProcedure
  224.  
  225. Define class = CocoaMessage(0, WindowID(0), "class")
  226. Define selector = sel_registerName("performKeyEquivalent:")
  227. class_addMethod(class, selector, @PerformKeyEquivalent(), "v@:@")
  228. selector = sel_registerName("flagsChanged:")
  229. class_addMethod(class, selector, @PerformKeyEquivalent(), "v@:@")
  230. selector = sel_registerName("keyUp:")
  231. class_addMethod(class, selector, @PerformKeyEquivalent(), "v@:@")
  232.  
  233. Repeat
  234.   Define ev.i = WaitWindowEvent()
  235.   If ev = #PB_Event_Gadget
  236.     Select EventGadget()
  237.       Case 0,1,2
  238.         activateSelector(EventGadget())
  239.       Case 3
  240.         SetGadgetText(0,#pressInvite)
  241.         HideGadget(3,#True)
  242.       Case 4
  243.         SetGadgetText(1,#pressInvite)
  244.         HideGadget(4,#True)
  245.       Case 5
  246.         SetGadgetText(2,#pressInvite)
  247.         HideGadget(5,#True)
  248.       Default
  249.         deactivateSelector()
  250.     EndSelect
  251.   EndIf
  252.   If ev = #PB_Event_LeftClick
  253.     deactivateSelector()
  254.   EndIf
  255. Until ev = #PB_Event_CloseWindow
RAW Paste Data