Capture double-tap of CTRL key?

Mac OSX specific forum
rr701
New User
New User
Posts: 4
Joined: Fri Nov 04, 2022 3:28 pm

Capture double-tap of CTRL key?

Post by rr701 »

This is an amazing forum!

I'm new to PB and I'm trying to do something that used to require 1 line of code in python - how would I capture the double-tap of a special key, such as CTRL or CMD or SHIFT?

I saw this feature https://superuser.com/questions/698261/ ... 62#1749562 in iTerm2 and would like to implement it in iCanHazShortcut.

Thank you all amazing folks so much!!
User avatar
deseven
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: Capture double-tap of CTRL key?

Post by deseven »

The problem lies much deeper and I'm afraid there is no simple solution.

iCHS uses this module to register hotkeys, which in turn uses native RegisterEventHotKey() method. I don't think this method supports double tapping or even registering hotkeys with a modifier key only. In order to achieve that you would need to monitor keyboard input (that's why iTerm2 requires a specific permission when you enable this functionality and that's also why iCHS doesn't require anything) and write custom logic around it. Yeah, native programming and Python are different :)

Maybe someone can provide a working example that reacts to at least a single modifier press across all windows, you might also find a hacky way to integrate it.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Capture double-tap of CTRL key?

Post by mk-soft »

Hello new user (I hope is not a new bot again)

For this you can use some cocoa function ...

Code: Select all

#NSLeftMouseUp        = 2
#NSRightMouseUp       = 4
#NSMouseMoved         = 5
#NSKeyDown            = 10
#NSKeyUp              = 11
#NSFlagsChanged       = 12
#NSScrollWheel        = 22

#NSAlphaShiftKeyMask = 1 << 16
#NSShiftKeyMask      = 1 << 17
#NSControlKeyMask    = 1 << 18
#NSAlternateKeyMask  = 1 << 19
#NSCommandKeyMask    = 1 << 20

EnableExplicit

Global sharedApplication = CocoaMessage(0, 0, "NSApplication sharedApplication")
Define currentEvent, type, modifierFlags, keyCode
Define clickCount, location.NSPoint, deltaX.CGFloat, deltaY.CGFloat
Define Event

Procedure.s modifierFlagsInfo(modifierFlags)
  Protected r1.s, flags
  flags = modifierFlags & $FFFF0000
  If modifierFlags & #NSShiftKeyMask
    r1 + "Shift "
  EndIf
  If modifierFlags & #NSControlKeyMask
    r1 + "Control "
  EndIf
  If modifierFlags & #NSAlternateKeyMask
    r1 + "Alt "
  EndIf
  If modifierFlags & #NSCommandKeyMask
    r1 + "Command "
  EndIf
  ProcedureReturn r1
EndProcedure

Global keyTime, lastKeyTime

keyTime = ElapsedMilliseconds()

If OpenWindow(0, 0, 0, 300, 100, "MacOS", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  TextGadget(0, 5, 5, 290, 25, "Info")
  
  Repeat
    
    Event = WaitWindowEvent()
    currentEvent = CocoaMessage(0, sharedApplication, "currentEvent")
    If currentEvent
      
      type = CocoaMessage(0, currentEvent, "type")
      modifierFlags = CocoaMessage(0, currentEvent, "modifierFlags")
      
      ; keyboard events
      Select type
        Case #NSKeyDown
          keyCode = CocoaMessage(0, currentEvent, "keyCode")
          SetGadgetText(0, modifierFlagsInfo(modifierFlags) + " ScanCode: "+ Str(keyCode))
          
        Case #NSKeyUp
          
        Case #NSFlagsChanged
          SetGadgetText(0, modifierFlagsInfo(modifierFlags))
          
          If (modifierFlags & $FFFF0000) = #NSCommandKeyMask
            keyTime = ElapsedMilliseconds()
            If keyTime - lastKeyTime < DoubleClickTime()
              Debug "Doppel Command "
            EndIf
            lastKeyTime = keyTime
          EndIf
          
      EndSelect
      
    EndIf
    
  Until Event = #PB_Event_CloseWindow
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
rr701
New User
New User
Posts: 4
Joined: Fri Nov 04, 2022 3:28 pm

Re: Capture double-tap of CTRL key?

Post by rr701 »

Thank you!!! I'll check it out next week. Also, definitely not a bot :+)

Thanks again!!
rr701
New User
New User
Posts: 4
Joined: Fri Nov 04, 2022 3:28 pm

Re: Capture double-tap of CTRL key?

Post by rr701 »

Thank you all!

Ivan, what do you think? Would it be possible to capture the CMD double tap with the proposed COCOA function?

Thank you all!!
rr701
New User
New User
Posts: 4
Joined: Fri Nov 04, 2022 3:28 pm

Re: Capture double-tap of CTRL key?

Post by rr701 »

I implemented it in python -- this binds the double tap of the right command key in MacOS.

Any suggestions on how this can either be called from PureBasic, or, better yet, achieved in PureBasic?

Thank you all so much!


Code: Select all

import keyboard
import time
import subprocess

lastime=0.0
pressed_once_or_more=False
MIN_INTERNVAL=0.4 #seconds
PROGRAM=['/usr/bin/open','/Applications/Safari.app']
SPECIAL_KEY='command'

print ("Welcome to a little utility function to bind SPECIAL_KEY+SPECIAL_KEY to launching a program")

while True:
    
    keyboard.wait(SPECIAL_KEY)
    t1=time.perf_counter()
    
    print("\nt1={}\nlasttime={}\n".format(t1,lastime))

    if (t1-lastime<MIN_INTERNVAL and pressed_once_or_more):
        print("******************CALLED!******************")
        subprocess.call(PROGRAM)

    lastime=t1
    pressed_once_or_more=True
Post Reply