trying to simulate fn keys from python in mac

306 Views Asked by At

I'm trying to simulate double fn keypress on mac to start dictation so far i have read all the docs on SO and applied a few solutions, but all in vain. I'm trying to open dictation from python code. executed from terminal. So far i'have tried this but with no success.

from Quartz.CoreGraphics import CGEventCreateKeyboardEvent
from Quartz.CoreGraphics import CGEventPost
from Quartz.CoreGraphics import kCGHIDEventTap

evt = [CGEventCreateKeyboardEvent(None, 0x3f, True), 
        CGEventCreateKeyboardEvent(None, 0x3f, False), 
        CGEventCreateKeyboardEvent(None, 0x3f, True), 
        CGEventCreateKeyboardEvent(None, 0x3f, False)]
CGEventPost(kCGHIDEventTap, evt)

can anyone tell me where i'm going wrong ?

1

There are 1 best solutions below

0
Алексндр Босов On

This code working successfully:

import Quartz, AppKit

ev = AppKit.NSEvent.otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(
     14,
     (0, 0),  # location
     0xa00,  
     0,
     0,
     0,
     8,
     ((0x3f - 128) << 16) | (0xa << 8),
     -1
     )
Quartz.CGEventPost(0, ev.CGEvent())

event = Quartz.CGEventCreateKeyboardEvent(None, 0x3f, True)
Quartz.CGEventSetFlags(event, 0)
Quartz.CGEventPost(Quartz.kCGHIDEventTap, event)

The problem was that you didn't specify the flags for the mouse event.The variable "ev" contains all the data where the button will be clicked.