Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8058455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:21:48+00:00 2026-06-05T09:21:48+00:00

Given a string, I’d like to be able to send a set of keystrokes

  • 0

Given a string, I’d like to be able to send a set of keystrokes to type that string and I’d like to be able to do it in python on OSX (in python because it’s part of a larger project already written in python, on OSX because I am trying to port it to OSX).

I’m able to do this now using pyobj like so (this is somewhat simplified):

from Quartz import *

CHAR_TO_SEQUENCE = {
    'a':[(0, True), (0, False)]
}

def send_string(string):
    for c in string:
        sequence = CHAR_TO_SEQUENCE[c]
        for keycode, key_down in sequence:
            CGEventPost(kCGSessionEventTap, CGEventCreateKeyboardEvent(None, keycode, key_down))

and I’ve fleshed out CHAR_TO_SEQUENCE to include most of what I can type on my keyboard, which took a while and was tedious.

The problems with this are:
– It only works while the keyboard has the ANSII standard layout. If someone uses a french keyboard, for example, it will type the wrong things.
– It requires this ridiculous table.

I found this general solution for OSX but couldn’t figure out how to apply it to python:
How to convert ASCII character to CGKeyCode?

The API mentioned there doesn’t seem to be available via pyobj (or maybe I just couldn’t figure out the import path).

I’ve seen some suggestions to set CGEventKeyboardSetUnicodeString to the desired string and not worry about the keycode. But I wasn’t able to figure out how to call CGEventKeyboardSetUnicodeString from python (I can’t get the arguments right) and it’s not clear that this will work because the documentation says that applications can choose to ignore this in favor of the keycode.

Is there a way to do this in python on OSX?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:21:49+00:00Added an answer on June 5, 2026 at 9:21 am

    It looks like the Carbon modules don’t wrap the TIS* functions, and neither does anything else.

    You could extend PyObjC, but it’s much simpler to just build a trivial extension module that wraps the two functions you actually need.

    Since it was faster to just do it than to explain how to do it, you can get it from https://github.com/abarnert/pykeycode and just do the usual “python setup.py build_ext –inplace” or “sudo python setup.py install”, then look at test.py to see how to use it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to check from python if a given string
For the given string example: <a><b><c><d><e><f><g> I'd like to write an expression that will
Given String // 1 2 3 String a = letters.1223434.more_letters; I'd like to recognize
I'm trying to compress any given string to a shorter version, copy paste-able compressed
I've been looking for a way to hash a given string in C# that
How do I create an array in smarty from a given string like 22||33||50
Consider a JavaScript method that needs to check whether a given string is in
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to validate that the given string contains contains only letters, numbers,
So I'm trying to write a method that reverses a given string but the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.