# Best way to send keystrokes to a 3rd party application?

**URL:** <https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469>\
**Category:** General\
**Created:** [March 4, 2024, 11:18am UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469 "2024-03-04T11:18:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Wheel](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_wheel/32/20536_2.png) [@Christian\_Wheel](https://forum.xojo.com/u/Christian_Wheel)\
**Post date:** [March 4, 2024, 11:18am UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/1 "2024-03-04T11:18:00Z")

</div>

I need a way to send keystrokes to a particular third party application on both Windows and Mac (the same third party app on both platforms).

I don’t know if an app can specifically be targeted for keystrokes or if I would need to somehow programatically bring it to the foreground, ensure it has focus, and then send the keystrokes.

Does anyone know if there is an existing accepted way to this in Xojo already, or is it declare digging time?

---

<div class="post-metadata">

**Author:** ![Anthony\_G\_Cyphers](https://forum.xojo.com/user_avatar/forum.xojo.com/anthony_g_cyphers/32/16862_2.png) [@Anthony\_G\_Cyphers](https://forum.xojo.com/u/Anthony_G_Cyphers)\
**Post date:** [March 4, 2024, 11:24am UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/2 "2024-03-04T11:24:48Z")

</div>

For Windows you can check Windows Functionality Suite for the SendKeys module. For macOS, I think @Christian_Schmitz has something.

---

<div class="post-metadata">

**Author:** ![Christian\_Wheel](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_wheel/32/20536_2.png) [@Christian\_Wheel](https://forum.xojo.com/u/Christian_Wheel)\
**Post date:** [March 4, 2024, 11:28am UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/3 "2024-03-04T11:28:45Z")

</div>

Thanks @Anthony_G_Cyphers, SendKeys requires the app to have focus, correct?

---

<div class="post-metadata">

**Author:** ![Anthony\_G\_Cyphers](https://forum.xojo.com/user_avatar/forum.xojo.com/anthony_g_cyphers/32/16862_2.png) [@Anthony\_G\_Cyphers](https://forum.xojo.com/u/Anthony_G_Cyphers)\
**Post date:** [March 4, 2024, 11:29am UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/4 "2024-03-04T11:29:20Z")

</div>

You call AppActivate (or whatever it’s called) on the window, then call the specific SendKeys function(s).

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [March 4, 2024, 11:37am UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/5 "2024-03-04T11:37:52Z")

</div>

Yes, we have a [RemoteControlMBS](https://www.monkeybreadsoftware.net/module-remotecontrolmbs.shtml) module. And **WinBringWindowToTop** may help with the focus.

---

<div class="post-metadata">

**Author:** ![Christian\_Wheel](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_wheel/32/20536_2.png) [@Christian\_Wheel](https://forum.xojo.com/u/Christian_Wheel)\
**Post date:** [March 4, 2024, 11:46am UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/6 "2024-03-04T11:46:12Z")

</div>

Thanks, @Christian_Schmitz, is there a Mac equivalent for WinBringWindowToTop? Playing around with RemoteControlMBS and MacProcessVisible = True makes the application window visible but does not give it the focus.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [March 4, 2024, 12:02pm UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/7 "2024-03-04T12:02:56Z")

</div>

You may just try to do that with an Apple Script.  
Or check [NSRunningApplicationMBS](https://www.monkeybreadsoftware.net/class-nsrunningapplicationmbs.shtml) class, where we have a [activateWithOptions](https://www.monkeybreadsoftware.net/process-nsrunningapplicationmbs-method.shtml) method.

---

<div class="post-metadata">

**Author:** ![Christian\_Wheel](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_wheel/32/20536_2.png) [@Christian\_Wheel](https://forum.xojo.com/u/Christian_Wheel)\
**Post date:** [March 4, 2024, 12:43pm UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/8 "2024-03-04T12:43:54Z")

</div>

Thanks Christian!

If anyone else needs a Mac version of AppActivate, here’s a quick and dirty method (requires MBS):

```auto
Public Function MacAppActivate(bundleID as String) As Boolean
#If TargetMacOS Then
  var n() as NSRunningApplicationMBS = NSRunningApplicationMBS.runningApplicationsWithBundleIdentifier(bundleID)
  if n.count = 0 then
    //App is not running
    Return false
  end if
  
  var u as integer, b as boolean = n(0).activateWithOptions(u)
  Return b
  #EndIf
End Function

```

---

<div class="post-metadata">

**Author:** ![Christian\_Wheel](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_wheel/32/20536_2.png) [@Christian\_Wheel](https://forum.xojo.com/u/Christian_Wheel)\
**Post date:** [March 4, 2024, 1:09pm UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/9 "2024-03-04T13:09:38Z")

</div>

Sending keys on Mac via RemoteControlMBS.MacPressKey worked the first time I ran the app in the IDE (when I was prompted to toggle the switch in Accessibility), but fails on each subsequent debug compile. I think perhaps because it no longer has system permissions to send keys, despite the switch still being toggled on in the Privacy & Security preference pane.

Is there a workaround for this?

---

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [March 4, 2024, 1:15pm UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/10 "2024-03-04T13:15:27Z")

</div>

I don’t know if this is any help to you, but I use third-party apps because of problems like this. Under Windows [AutoHotkey](https://www.autohotkey.com/) and on the Mac [Keyboard Maestro](https://www.keyboardmaestro.com/main/).

---

<div class="post-metadata">

**Author:** ![Christian\_Wheel](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_wheel/32/20536_2.png) [@Christian\_Wheel](https://forum.xojo.com/u/Christian_Wheel)\
**Post date:** [March 4, 2024, 1:22pm UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/11 "2024-03-04T13:22:31Z")

</div>

Thanks for this, does this require the user of your app to have Keyboard Maestro?

---

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [March 4, 2024, 1:30pm UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/12 "2024-03-04T13:30:36Z")

</div>

Sadly, yes. AutoHokey creates selfcontained EXE’s, but Keyboard Maestro needs an installed and licensed Keyboard Maestro Engine on each Machine.

---

<div class="post-metadata">

**Author:** ![Christian\_Wheel](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_wheel/32/20536_2.png) [@Christian\_Wheel](https://forum.xojo.com/u/Christian_Wheel)\
**Post date:** [March 4, 2024, 1:34pm UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/13 "2024-03-04T13:34:31Z")

</div>

Ahhh, that’s too bad. Won’t be able to swing that for my app, but I appreciate the suggestion. It looks like it’d be a great solution for apps that don’t need wide distribution.

---

<div class="post-metadata">

**Author:** ![Christian\_Wheel](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_wheel/32/20536_2.png) [@Christian\_Wheel](https://forum.xojo.com/u/Christian_Wheel)\
**Post date:** [March 4, 2024, 1:54pm UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/14 "2024-03-04T13:54:42Z")

</div>

> [@Christian\_Wheel](#):
>
> Sending keys on Mac via RemoteControlMBS.MacPressKey worked the first time I ran the app in the IDE (when I was prompted to toggle the switch in Accessibility), but fails on each subsequent debug compile. I think perhaps because it no longer has system permissions to send keys, despite the switch still being toggled on in the Privacy & Security preference pane.
> 
> Is there a workaround for this?

There is definitely something funky going on. The RemoteControlMBS.MacPressKey methods have just stopped working, even in the MBS Example Project “Write Something” where it was certainly working just an hour ago with no code changes. If anyone has any insight into what might have changed between runs, I’d appreciate it.

Edit: Okay so it seems that EVERY TIME YOU DEBUG YOUR APP, you have to open System Settings … Privacy & Security… Accessibility, and remove the old entry for your app’s permission. Then you have to let your app re-try its key sending operation, fail, allow the permission for the new instance of your app in Accessibility, and then try again.

This is a huge waste of time. I’m often running dozens of debugs an hour. Does anyone know a way to circumvent this?

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [March 4, 2024, 2:24pm UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/15 "2024-03-04T14:24:26Z")

</div>

I’ve been wrestling with this for days and I haven’t found a good workaround yet that doesn’t involve some serious screwing around with crazy AppleScripts, etc. This is a huge blind spot on Apple’s part for developers… I appreciate that they are focused on security but there ought to be a way to temporarily disable all the checks.

---

<div class="post-metadata">

**Author:** ![Christian\_Wheel](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_wheel/32/20536_2.png) [@Christian\_Wheel](https://forum.xojo.com/u/Christian_Wheel)\
**Post date:** [March 4, 2024, 2:33pm UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/16 "2024-03-04T14:33:57Z")

</div>

It also makes me wonder if those permissions will break whenever you push an update for your app.

---

<div class="post-metadata">

**Author:** ![Christian\_Wheel](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_wheel/32/20536_2.png) [@Christian\_Wheel](https://forum.xojo.com/u/Christian_Wheel)\
**Post date:** [March 4, 2024, 4:01pm UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/17 "2024-03-04T16:01:23Z")

</div>

@Eric_Williams Here’s a little something I put together after spending too much time on this issue, lol. Call this method when your app loads and it will remove the old permissions. You will still be prompted to allow it the next time you use accessibility features, but at least it saves you from having to remove it each time.

```auto
Public Sub ResetAccessibility()
  #If DebugBuild And TargetMacOS Then
    Declare Function mainBundle Lib "AppKit" selector "mainBundle"(NSBundleClass As Ptr) As Ptr
    Declare Function NSClassFromString Lib "AppKit"(className As CFStringRef) As Ptr
    Declare Function getValue Lib "AppKit" selector "bundleIdentifier"(NSBundleRef As Ptr) As CFStringRef
    var bundleID As String = getValue(mainBundle(NSClassFromString("NSBundle")))
    var s as new Shell
    s.Execute("tccutil reset All " + bundleID)
  #EndIf
End Sub

```

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [March 4, 2024, 5:06pm UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/18 "2024-03-04T17:06:31Z")

</div>

> [@Christian\_Wheel](#):
>
> This is a huge waste of time. I’m often running dozens of debugs an hour. Does anyone know a way to circumvent this?

For Full Disk Access and Automation I need a built app so that I can do debugging.

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [March 4, 2024, 7:17pm UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/19 "2024-03-04T19:17:47Z")

</div>

A million thanks for this; you just saved me hours of frustration.

---

<div class="post-metadata">

**Author:** ![Anthony\_Dellos](https://forum.xojo.com/user_avatar/forum.xojo.com/anthony_dellos/32/208_2.png) [@Anthony\_Dellos](https://forum.xojo.com/u/Anthony_Dellos)\
**Post date:** [March 4, 2024, 7:20pm UTC](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469/20 "2024-03-04T19:20:03Z")

</div>

Here. Here. Great stuff @Christian_Wheel

[Next page](https://forum.xojo.com/t/best-way-to-send-keystrokes-to-a-3rd-party-application/79469.md?page=2)
