Swift iOS - simulate a keystroke from a HARDWARE KEYBOARD

In an iOS app I have (which can also run on an M1 Mac under macOS) there is this routine.

It captures the keystrokes from the DESKTOP keyboard (in mac Mode) as opposed to using the “Virtual Keyboard” for iOS

Anyway this routine returns information I need about the key just pressed, information that so far seems to only be available via this routine.

Now I do KNOW all the “scan” values already, what I DON’T know is the unicode value for each scan value when combined with all the possible modifiers (Shift, alt, cmd, ctrl etc)

I have been trying to find a method that will “simulate” those keypresses, so I can create a table of those values.

I could do it by tediously typing each character and modifier on the actual keyboard, but that is close to 500 combinations

 override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
   guard let key  = presses.first?.key else { return }
   let scan  = "0x"+String(format:"%02x",key.keyCode.rawValue)
   let mod   = "0x"+String(format:"%08x",key.modifierFlags.rawValue)
   let unicode = "0x"+String(format:"%04x",Asc(key.characters))
   if unicode != "0x0000" {
      print("Scan=[\(scan)] Mod=\(mod)  asc=\(unicode) Char=[\(key.characters)]")
   }
 }

would Apple Script / automator help on this?

Perhaps it is worth to search github for any existing scripts …

neiter of those work in an iOS app

Maybe iOS Shortcuts app might help?

I know, but I hoped that it might work with the simulator / or running the app on your macOS. Never tried that though. So obviously the answer is no :frowning:

If this were a true multi-platform app, that might have been an option…
but there are now multiple formats to write/run apps on macOS

  1. write and compile directly for macOS
    this requires 100% of the syntax match the AppKit standards and macOS functions

  2. write and compile for iOS and macOS native (compiler directives required)
    this requires the code meet both UIKit and AppKit standards with iOS and macOS functions

  3. write and compile directly for iOS (with option to run on any M1 Mac),
    this requires the code meet only UIKit standards and runs in a “window” on M1-Mac, with only iOS functions

  4. write as a Catalyst app… never done one, so not sure the requirements

This app in question is under #3