0

I'm trying to manipulate the frontMostApplication in macOS and send unicode characters to it. For example when writing text.

So far, my approach has been to manipulate and simulate the keyboard like so

let active = NSWorkspace.shared.frontmostApplication

let space = CGEventSource(stateID: .hidSystemState)
let ev = CGEvent(keyboardEventSource: space, virtualKey: 0 as CGKeyCode, keyDown: true)
if let pid = active?.processIdentifier{
  ev?.postToPid(pid)
}

Which will allow me to push an ascii a to the app. I'm wondering how I can insert other characters, such as unicode emojis. I might be able to use ALT + combinations for unicode, but that seems tediously cumbersome, and I would expect there to be a better way.

Thanks!

mikkelam
  • 517
  • 1
  • 7
  • 15
  • Does this answer your question? [How to insert text at cursor position of another application on Mac OSX Application (like OSK)?](https://stackoverflow.com/questions/16272717/how-to-insert-text-at-cursor-position-of-another-application-on-mac-osx-applicat) – Willeke Jan 12 '22 at 05:26
  • @Willeke in principle, yes! Though I cant accept the limit of not working in web apps. If this is the only "native way" then i guess keystroking is my best option – mikkelam Jan 12 '22 at 08:53

0 Answers0