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

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
This Print Screen software is mp3blaster - learn more..
This Print Screen is a GUI what are developer are work:
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:
Windows Script Host 2.0 beta (which you can download from
http://msdn.microsoft.com/scripting/windowshost/beta/) includes a COM
object that implements a SendKeys function.
tcom 1.7 (available at http://www.vex.net/~cthuang/tcom/) is a
Windows-specific Tcl extension that allows Tcl scripts to access
COM objects.
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.

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.

To generate a key, you need the event generate command.
event generate $w <KeyPress-F1> -x $x -y $y
You probably want to direct the event to the focus window, and the X and Y locations ought to be set to 0,0 if you don't have a better place (or alternatively use the -rootx and -rooty options and the winfo pointerxy command to use global coordinates).
There are many other fields you can set for a key-press event, but most of them aren't commonly used. (In particular, I think it's easier to set up the state field using modifiers to the event being generated rather than explicitly setting the -state option.)

Related

Get terminal contents [duplicate]

This question already has an answer here:
Read screen character from terminal
(1 answer)
Closed 2 years ago.
Is it possible to get a snapshot of the text contents of a Linux terminal?
Both tput and terminfo support "cup" mode (e.g. tput smcup to start alternate buffer mode) which implies they must save the screen state somewhere. Is it possible to get these contents?
You can save a snapshot of the text contents of a Linux terminal to
a file using GNU Screen
hardcopy
feature but only if you started screen beforehand. Apart from that,
you can restore terminal contents saved with tput smcup using
tput rmcup but this works only in xterm (not only in xterm
terminal emulator itself but also in other terminal emulators
providing that $TERM is set to xterm. It doesn't work in Linux TTY
though).
Four Methods to Take Screenshot Capture in Ubuntu Linux
Use Print Screen
This is the most common method to take screenshots. Pressing the “Print Screen” button will take the screenshot of the “Entire Visible Screen”.
When we want to take a particular window, we can use “Alt+Print Screen”. Alt+PrintScreen will take only the particular window which is currently active.
Use gnome-screenshot
gnome-screenshot utility is part of the GNOME Desktop Environment, which can also be used to take screenshot. It also has a command line mode (gnome-screenshot)
From the command-line, just type the command “gnome-screenshot” to do the same. The command will take a screenshot and provide a dialog to save it.
$ gnome-screenshot
Capture Only the Current Window:
From the UI, to take the screenshot of the current active window alone, select “Grab the Current Window” and click “Take Screenshot”.
From the command-line, use the -w option as follows to do the same.
$ gnome-screenshot -w
Take Screenshot After Some Delay:
From the UI, you can also set a delay before taking the screenshots. Set the “Grab after a delay” to the required number of seconds. This will be really helpful when we need to take screen shots of navigation.
From the command-line, use -d option to do the same. -d 2 is used for delaying the screenshot for 2 seconds. So within the 2 seconds, we can make the window which we want to take screenshot as active.
$ gnome-screenshot -w -d 2
Capture a Particular Area:
From the UI, if you want to take a particular rectangle area alone, then select “Grab a Particular area” and click “Take Screenshot”.
From the command-line, use the -a option to do the same. Once this command is entered, the mouse pointer will be changed, and you can drag and select which area to take screenshot.
$ gnome-screenshot -a
Take Screenshot Including or Excluding Window Border:
From the UI, you can also include or exclude the window border by selecting/deselecting “Include the Window Border” option.
Use ImageMagic’s Import Command
ImageMagick is an open source software suite for displaying, converting, and editing raster image files. It comes with various command line tools, and one of that is “import”. Now we will see, how we can use import to take screenshots. You can install it by using apt-get on debian/ubuntu as follows:
apt-get install imagemagick
Capture Entire Screen using -window root option
Use the “-window root” option to take screenshot of the complete screen. The screenshot will be saved in the file name provided in the command line.
$ import -window root Pictures/Image5.png
ImageMagick supports more that 100 file types. You can use any one of them to store the output.
Use GIMP
You can also take screenshot from gimp. Launch gimp, and click “File->Create->Screenshot”. A new dialog window will open with options similar to gnome-screenshot.

How can I enter text into Launchy and append it to the end of a text file?

I'm a Debian Stretch user coming from Windows. In Windows with the launchy app (also available for Linux), I had a method of entering text into launchy that was then appended to the end of a .txt or .md file.
To do this in Windows, I created a file called note.bat that contained the following:
echo %*>>"C:\collectednotes.md"
I'd make launchy aware of note.bat by adding its containing folder to “Launchy” → “Settings” → “Catalog” and adding filetype *.bat.
From there, I'd launch launchy, type note, hit Tab, enter some text, hit Enter, and then the text would be added to the end of collectednotes.md.
A mostly working process is detailed in my answer below. I'll give the green checkmark answer to anyone that can adjust this process (via note.sh and/or launchy plugin setup detailed below) to appropriately handle all special characters.
This may contain the solution to this question:
Which characters need to be escaped in Bash? How do we know it?
Solved (almost). I'm keeping this question unanswered and will give to whoever completes the remaining ~5%. Steps to get the 95% solution with xfce4-terminal version 0.8.3-1:
Install launchy and launchy-plugins (both are version 2.5-4 for me):
apt-get install launchy
apt-get install launchy-plugins
Open terminal to default location of ~/ and create collectednotes.md:
echo "# Launchy Notes Collected Here" > collectednotes.md
Create note.sh shell script:
echo '#!/bin/sh' > note.sh
Create shell script line 2:
echo ALL_ARGUMENTS='"$#"' >> note.sh
Create shell script line 3:
echo 'echo "$ALL_ARGUMENTS" >> ~/collectednotes.md' >> note.sh
If you open note.sh, it will look like:
#!/bin/sh
ALL_ARGUMENTS="$#"
echo "$ALL_ARGUMENTS" >> ~/collectednotes.md
Make note.sh executable:
chmod +x note.sh
Launch launchy and click the gear icon in upper right for settings. If consistency with launchy for Windows is desired, set launchy Hotkey to Alt+Space. If you receive a keyboard shortcut conflict message as I do on Debian with Xfce, first go to Settings, Window Manager, Keyboard tab, and clear Alt+Space as the shortcut for Window operations menu.
Next in launchy settings, go to Plugins tab and enable the plugin Runner. Click the + button and create a new Runner custom command as follows:
- Name: note
- Program: /home/YOURUSERNAMEHERE/note.sh (launchy does not like Program path of ~/note.sh, so a specific path with username is required)
- Arguments: '$$'
Click the Catalog tab and hit Rescan Catalog just in case. Hit OK to close launchy settings.
Now let's test it.
- launch launchy with Alt+Space or your hotkey
- type note (You may have to wait 10 second or so on first run. You'll know things are as expected when you see the text "note" with a orange/yellow icon containing silhouette of a person.)
- hit Tab
- enter some text (no need for single or double quotes or escapes), e.g.: Remember to donate at least $3 to Josh at Launchy https://www.launchy.net/donate.php
- hit Enter
Now open collectednotes.md to confirm the text was captured.
The remaining issues seem to be dealing with single quotes and double quotes. For example, consider the following note:
I don't know what I'd do without Launchy.
Which results in the following in collectednotes.md:
I dont know what Id do without Launchy.
Or:
Would David Allen like universal text capture from anywhere in Linux? My bet is "yes!"
Results in the following in collectednotes.md:
Would David Allen like universal text capture from anywhere in Linux? My bet is \yes!\
Single quoting and/or double quoting the input to launchy doesn't solve it. Note the launchy Runner custom plugin construction component of '$$' is a piece of this puzzle.
I'll give the answer to anyone that can adjust this process (via note.sh and/or launchy plugin setup) to appropriately handle all special characters. Maybe this would add proper escapes to user input with something like gsub.
The rationale for being exacting regarding proper character handling is that this process is useful for copying and logging random chunks of text from web pages, but if common characters like single quotes are not handled as expected, confidence in the system is much reduced.

Can GNOME Shell extensions move the pointer? If so, how?

I want to write an extension that does the opposite of the "focus-follows mouse" setting in GNOME Shell: I want to make my pointer move to the center of the currently focused window.
Can this be done in a GNOME Shell extension? I see some GNOME code that wraps xfixes cursor, but I can't find any references to programmatic pointer updates in either the core Javascript or any existing extensions. (Am I just bad at Google?)
Valid answers include (1) example code that does it or (2) citation of a canonical source that says it can't be done.
Found this code in overview.js
Gdk = imports.gi.Gdk
let display = Gdk.Display.get_default();
let deviceManager = display.get_device_manager();
let pointer = deviceManager.get_client_pointer();
let [screen, pointerX, pointerY] = pointer.get_position();
pointer.warp(screen, 10, 10);
Are you willing to write your own script? If you are, I have found three tools, which, if used together, can get the job done for you.
First, use xprop to get the PID of the window you have clicked on.
Next, use xwininfo to get the dimensions and position information of the window based on its process ID.
Finally, use xdotool to calculate the center position of said window and move the cursor to that exact position.
Hope this helps. I don't have enough time write now to write the script (sorry), but this should be enough to get you started.
EDIT: Based on your comment, you want to stay in GNOME js. Totally understandable. You can call xdotool (which is the most efficient way of changing the position of the cursor on screen) from within GNOME js by use of something like:
const Util = imports.misc.util;
Util.spawn(['/bin/bash', '-c', "xrandr --query | awk 'something'"]) # replace the code here wih your own
This code was found at this thread.

xterm dump of full scrollable window content

I want to know if anyone does know a way to dump or copy the whole lot of viewable messages in a xterm window on linux. The very important thing is I don't want to know how to send a command out and kapture its output for stream 1 and 2 as well as the input, as this is well known to me.
I may explain for what this is needed. You do something and expect not any complications but than you got pages of msg's als err msg or normal output. To be able to see later after it you should be able to get them in a file and as long as you are able to scroll that all back and forther with your mouse it is sure the data is there some where. But the time may be not to scroll and screenshot and scroll ....
I would be glad to help me out in such cases and it would be fine to have the full view including all your own typing and all the msg's in same order as you watch it when you scroll it back.
I don't really know where this is stored and how you could get that saved. I know that I could dump the whole lot of Memory and search it for a part of the xterm window, but that is a bit over the top I think.
There is a control sequence, which I had forgotten. This question reminded me. In XTerm Control Sequences, it is noted "print all pages":
CSI ? Pm i
Media Copy (MC, DEC-specific).
Ps = 1 -> Print line containing cursor.
Ps = 4 -> Turn off autoprint mode.
Ps = 5 -> Turn on autoprint mode.
Ps = 1 0 -> Print composed display, ignores DECPEX.
Ps = 1 1 -> Print all pages.
That dates from 1999 (patch #119), so you likely have it in your xterm. You could do this in a shell command like this:
printf '\033[?11i'
A comment mentions the page Hidden gems of xterm, which uses the corresponding action print-everything (something that can be executed via the translations resource). It is in the manual page, of course. The same comment points to Extra characters in XTerm printerCommand output, which mentions the resource printAttributes. By default, the control sequences for the printer tell xterm to send extra control characters (to reconstruct video attributes). The resource can be modified (set to 0) to suppress that. That is even older (patch #74).
Without that — Conceivably one could construct an application which used the X SendEvent protocol to construct a series of events which would be interpreted as xterm actions to scroll back, select text and copy it chunk-by-chunk via the clipboard. You could even write it in Perl (there is a module for X protocol). But seriously, no.
If you want to capture text which was written to xterm, you can do this by preparing before the text is needed by different methods (see manual):
turn on the xterm logging feature (not that user-friendly because it generates the filename). This can be enabled using the "Log to File (logging)" menu entry.
use the printer control sequences to write lines as they are written (again, not that friendly, though there is a menu entry to turn it on and off, "Redirect to Printer (print-redir)")
use script to capture all output to the terminal. I use this, because it works with any terminal on any POSIX-like system (even Cygwin).
Each of these methods produces a file containing escape/control sequences, which requires filtering out. The hypothetical program using SendEvent could in principle eliminate that.

How to simulate keyboard key press (+Code ASCII of 'ALT' key)

I recently understood that if I write characters inside /dev/tty it will simulate key presses with a keyboard.
I was wondering if I can simulate holding a key or a key combination.
What I'm looking for is to simulate the Alt + Tab to switch between open applications.
I'm not looking for alternatives. I am practising to learn how can I simulate a physical key press on a keyboard.
Is it possible to simulate key combinations (like Alt + Tab)?
Also, how can I press the Enter key?
It seems it's not '\n' or '\r' or "\r\n" or "\n\r".
The reason I'm looking for the Enter key is that I want do a "sudo" command in the terminal and when it prompts for a password, I use the /dev/tty to answer that, but it seems standard input or standard output (using "-S" option with sudo) don't work.
Is there anything I'm doing wrong?
Are there some libraries I should be looking for (like Expect)? Or is what I'm trying to do impossible?
I am using C++. All I want to do is to try to simulate a physical key press on my keyboard. The OS is Ubuntu 13.04 (Raring Ringtail). (Just as a practise)
But I have two goals:
To create a program which switches between applications using Alt + Tab.
interact with a Linux terminal.
I've found two solutions:
Write characters/codeASCII s of the corresponding keys to /dev/tty
Write characters/codeASCII s of the corresponding keys to standard output and/or standard input
But none of them worked. Is what I'm doing correct or not?
X does not read keystrokes from /dev/tty. If you want to simulate keystrokes in X, you’ll need to send them another way. If you’re writing a shell script, xdotool is your friend; if you’re writing C, though, you’ll probably want to use libxdo, the library underlying xdotool. If you’re looking for something a bit more low-level, you might be interested in the XCB Test API, on which both xdotool and libxdo rely.
Unless you can provide more information about your experience with sudo, I don’t think anybody will be able to answer your second question. sudo -S is definitely the intended mechanism for programmatically providing a password to sudo.

Resources