Page 1 of 1

Simulate Keyboard

Posted: Tue Dec 19, 2023 8:20 pm
by spacebuddy
Hi all,

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

Any help would be appreciated.

Thanks

Re: Simulate Keyboard

Posted: Fri Dec 22, 2023 12:58 am
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……

Re: Simulate Keyboard

Posted: Fri Dec 22, 2023 1:41 am
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


Re: Simulate Keyboard

Posted: Wed Jan 10, 2024 12:44 pm
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

Re: Simulate Keyboard

Posted: Thu Jan 11, 2024 2:39 pm
by Piero
Small update to post above :wink:

Re: Simulate Keyboard

Posted: Thu Jan 11, 2024 5:37 pm
by mk-soft
I don't believe in constantly writing files and then calling them up externally as a script.

Re: Simulate Keyboard

Posted: Thu Jan 11, 2024 6:32 pm
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

Re: Simulate Keyboard

Posted: Sat Jan 13, 2024 7:49 pm
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...

Re: Simulate Keyboard

Posted: Sat Jan 13, 2024 8:00 pm
by mrbungle
Apologies.

Re: Simulate Keyboard

Posted: Sat Jan 13, 2024 9:12 pm
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!

Re: Simulate Keyboard

Posted: Sat Jan 13, 2024 10:04 pm
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