Simulate Keyboard
-
- Enthusiast
- Posts: 356
- Joined: Thu Jul 02, 2009 5:42 am
Simulate Keyboard
Hi all,
Is it possible to simulate CMD-A keyboard using code?
Any help would be appreciated.
Thanks
Is it possible to simulate CMD-A keyboard using code?
Any help would be appreciated.
Thanks
Re: Simulate Keyboard
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.
Re: Simulate Keyboard
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
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
Small update to post above 

Re: Simulate Keyboard
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Simulate Keyboard
Hey mk, you know I'm your fan etc., but that's just passing parameters to a shell, like runprogram do.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.
PS: Gestalten
Re: Simulate Keyboard
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 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.
Re: Simulate Keyboard
Apologies.
Re: Simulate Keyboard
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
Oh, well, to tell the truth I'm already enlightened; I post stuff just to have some laughs with Buddha