Skip to content
Advertisement

Invoking keyboard keys, through proc – procedure (function) in tcl

In front of the internet, I could only find so far, just the reverse where the key-related event is associated with the front-end button, not the front-end button that invokes a key. Understood ?! Look:

#!/usr/bin/env wish

button .b1 -text Hello -underline 0
button .b2 -text World -underline 0
bind . {.b1 flash; .b1 invoke}
bind . {.b2 flash; .b2 invoke}
pack .b1 .b2

Explanation – If you press a H button, the Hello button will blink. And the same effect happens with W; World

I want the Front-end [GUI] button to simulate the keystroke, for example – F1

enter image description here

This Print Screen software is mp3blaster – learn more..


This Print Screen is a GUI what are developer are work:

enter image description here

I asked that question a week or two ago. Since I did not get any plausible answer, I do not know if it is possible, or no one knows, or nobody cares.

I want to send a non tcl / tk application under UNIX GNU / Linux some keys for a very simple toolbar control. For the application must be the same as if these keys were hit on the keyboard. In VisualBasic The function is called SendKey. Is it possible to do this with tcl / tk?

I will describe how to do this using Windows Script Host and tcom:

In this example, the Tcl script starts Notepad and sends keys to the application.

package requires tcom

configure wshShell [:: tcom :: bind "WScript.Shell"]
set taskId [$ wshShell Run "notepad.exe"]
$ wshShell AppActivate $ taskId
after 500
$ wshShell SendKeys "The quick brown fox jumped  n"
$ wshShell SendKeys "{TAB} about the lazy dog."
:: tcom :: release $ wshShell

On UNIX; Linux how to accomplish this ??? It would be by focus method, inverse bind or third party application xvkbd, xbindkeys, xkeycaps, xev, xjoypad etc … Which to use and how to use if tcl/tk does not have one built-in command?!

If somebody knows and/or can clarify me, I’ll be grateful.

Advertisement

Answer

xdotool (http://www.semicomplete.com/projects/xdotool/) could help with that task.

Here’s a short snippet that sends Ctrl+Tab to Chrome when clicking a Tk button:

#!/usr/bin/env wish

#
# Focus window named $window_name and send the specified key sequence.
#
proc send_key {window_name key_sequence} {
    foreach wid [exec xdotool search --name $window_name] {
        if {![catch {exec xdotool windowfocus $wid}]} {
            exec xdotool key $key_sequence
        }
    }
}

button .f1 
    -text "Send Ctrl+Tab to Chrome (cycle through browser tabs)"  
    -command {send_key chrome ctrl+Tab}
pack .f1

It’s a bit problematic that you have to focus the window before you can send key sequences to it.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement