Simulate Keyboard

Mac OSX specific forum
spacebuddy
Enthusiast
Enthusiast
Posts: 356
Joined: Thu Jul 02, 2009 5:42 am

Simulate Keyboard

Post by spacebuddy »

Hi all,

Is it possible to simulate CMD-A keyboard using code?

Any help would be appreciated.

Thanks
User avatar
Piero
Addict
Addict
Posts: 865
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Simulate Keyboard

Post by Piero »

Code: Select all

Procedure simpleShell(ShellCommand$)
   Protected shell = RunProgram("/bin/sh","","", #PB_Program_Open|#PB_Program_Write)
   If shell
      WriteProgramStringN(shell,ShellCommand$)
      WriteProgramData(shell,#PB_Program_Eof,0)
      CloseProgram(shell)
   EndIf
EndProcedure

simpleShell(~"osascript -e 'tell app \"System Events\" to keystroke \"a\" using command down'")
; You can keystroke almost anything (text with returns, tabs, symbols etc.)
; "using" is only for shortcuts, example: using {command down, shift down}
; For MUCH more, see System Events' dictionary in Script Editor

; Give accessibility permissions in system prefs if it doesn't work……
Last edited by Piero on Thu Jan 11, 2024 2:37 pm, edited 1 time in total.
mrbungle
Enthusiast
Enthusiast
Posts: 143
Joined: Wed Dec 30, 2020 3:18 am

Re: Simulate Keyboard

Post by mrbungle »

This is old code found on the PB Forum. Whether it still works is another story but it should do what you are looking for or close to it.

Code: Select all

EnableExplicit

#kCGHIDEventTap= 0

#kVK_ANSI_E = $0E
#kVK_ANSI_H = $04
#kVK_ANSI_L = $25
#kVK_ANSI_O = $1F
#kVK_Return = $24


#kCGEventFlagMaskShift     = $020000 ; = NX_SHIFTMASK
#kCGEventFlagMaskControl   = $040000 ; = NX_CONTROLMASK
#kCGEventFlagMaskAlternate = $080000 ; = NX_ALTERNATEMASK
#kCGEventFlagMaskCommand   = $100000 ; = NX_COMMANDMASK

ImportC ""
  CFRelease(CFTypeRef.I)
  CGEventCreateKeyboardEvent(CGEventSourceRef.I, CGVirtualKeyCode.U, KeyDown.L)
  CGEventPost(CGEventTapLocation.I, CGEventRef.I)
  CGEventSetFlags(CGEventRef.I, CGEventFlags.L)
EndImport

Procedure.L SendKey(VirtualKey.U, ModifierKey.L = 0)
  Protected KeyEvent.I

  KeyEvent = CGEventCreateKeyboardEvent(0, VirtualKey, #True)

  If KeyEvent
    ; ----- Press key
    CGEventSetFlags(KeyEvent, ModifierKey)
    CGEventPost(#kCGHIDEventTap, KeyEvent)
    CFRelease(KeyEvent)

    KeyEvent = CGEventCreateKeyboardEvent(0, VirtualKey, #False)
    CGEventSetFlags(KeyEvent, ModifierKey)

    If KeyEvent
      ; ----- Release key
      CGEventPost(#kCGHIDEventTap, KeyEvent)
      CFRelease(KeyEvent)
    EndIf
  EndIf

  ProcedureReturn KeyEvent
EndProcedure

Define Msg.S = "Hello!"
Define ProgramID.I

If CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"),
  "launchApplication:$", @"TextEdit")
  ; ----- Wait until TextEdit is running (increase delay if necessary)
  Delay(500)
  ; ----- Send Keys to TextEdit
  SendKey(#kVK_ANSI_H, #kCGEventFlagMaskShift)
  SendKey(#kVK_ANSI_E)
  SendKey(#kVK_ANSI_L)
  SendKey(#kVK_ANSI_L)
  SendKey(#kVK_ANSI_O)
  SendKey(#kVK_Return)
EndIf

User avatar
deseven
Enthusiast
Enthusiast
Posts: 367
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: Simulate Keyboard

Post by deseven »

In case you just want to select all text this could be done with API calls instead of keystroke emulation.

Code: Select all

#exampleText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consectetur tortor nunc, eu facilisis justo laoreet et. Nunc at gravida ipsum, ut auctor sem. Nam malesuada purus at dolor lacinia."

OpenWindow(0,0,0,400,300,"Select all example",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
StringGadget(0,10,10,380,20,#exampleText)
EditorGadget(1,10,40,380,250)
AddGadgetItem(1,-1,#exampleText)

; select all for StringGadget
CocoaMessage(0,GadgetID(0),"selectText:",0)

; select all for EditorGadget
Range.NSRange\location = 0
Range\length = Len(#exampleText)
CocoaMessage(0,GadgetID(1),"setSelectedRange:@",@Range)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
User avatar
Piero
Addict
Addict
Posts: 865
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Simulate Keyboard

Post by Piero »

Small update to post above :wink:
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Simulate Keyboard

Post by mk-soft »

I don't believe in constantly writing files and then calling them up externally as a script.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Piero
Addict
Addict
Posts: 865
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Simulate Keyboard

Post by Piero »

mk-soft wrote: Thu Jan 11, 2024 5:37 pm I don't believe in constantly writing files and then calling them up externally as a script.
Hey mk, you know I'm your fan etc., but that's just passing parameters to a shell, like runprogram do.

PS: Gestalten
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Simulate Keyboard

Post by Shardik »

mrbungle wrote: Fri Dec 22, 2023 1:41 am This is old code found on the PB Forum. Whether it still works is another story but it should do what you are looking for or close to it.
It would have been nice if you also would have posted the link to your code. Often the thread contains additional information which might further help the readers of this thread...
mrbungle
Enthusiast
Enthusiast
Posts: 143
Joined: Wed Dec 30, 2020 3:18 am

Re: Simulate Keyboard

Post by mrbungle »

Apologies.
User avatar
Piero
Addict
Addict
Posts: 865
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Simulate Keyboard

Post by Piero »

mrbungle wrote: Sat Jan 13, 2024 8:00 pmApologies.
No way! Don't feel bad: you just didn't know/had to learn more; that is always happening to all of us!
User avatar
Piero
Addict
Addict
Posts: 865
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Simulate Keyboard

Post by Piero »

Piero wrote: Sat Jan 13, 2024 9:12 pmthat is always happening to all of us!
Oh, well, to tell the truth I'm already enlightened; I post stuff just to have some laughs with Buddha
Post Reply