Keyboard script for pressing a key - linux

I am looking for a simple linux program that will allow my keyboard to press a key for few seconds and then another one for few other seconds.
Thank you.

Check out xdotool, which lets you simulate keyboard input.
sudo apt-get install xdotool
#!/bin/bash
for i in `seq 0 9`;
do
xdotool key $i
done
UPDATE
To simulate a pressing a key for six seconds in Bash, then simulate pressing a different one for a further six seconds will be painful.
Therefore, I would use Ruby (you might have to install this from the repositories).
ruby -e 't = Time.new.to_i; key = "a"; while true do `xdotool key #{key}`; key = "b" if Time.new.to_i > t + 6; break if Time.new.to_i > t + 12; end'
If you run this from the command line it will simulate pressing key "a" for six seconds, after which it'll simulate a pressing key "b" for a further six.

Related

Why doesn't shift insert work in an xterm in Cygwin/X?

shift insert works in mintty, but when I launch X with
/usr/bin/startxwin -- -xkblayout "us" -xkbmodel "pc105" -xkbvariant "altgr-weur" -xkboptions "altwin:meta-win,compose:rctrl,ctrl:nocaps" -listen tcp&
xterm only shows 2~ when pressing shift insert. How do I make this work correctly? Do I have to configure something special in .inputrc or .XResources to make this work? I never changed any of the cygwin defaults on this.
shift insert works as intended in emacs under X.
Try this :
xrdb -merge << EOF
XTerm*VT100.translations: #override\n\
!Shift<Key>Insert : insert-selection(CLIPBOARD,PRIMARY)
EOF
I used xev to check the keycode. The windows On-Screen Keyboard returns 106 when I do a shift insert while the physical keyboard returns 90. Keycode 106 = Insert while keycode 90 = KP_Insert.
I only need a xmodmap -e 'keycode 90 = Insert" on my machine :)

Display a running clock in terminal using bash without using loop

How to display a running clock in a linux terminal without using any for or while loop. Using these loops in scripts with 1 sec duration causes a significant load in the systems.
How about:
watch -n 1 date
Use watch to run a command periodically.
You can make a function which calls itself with a sleep :
#!/bin/bash
function showdate(){
printf '\033[;H' # Move the cursor to the top of the screen
date # Print the date (change with the format you need for the clock)
sleep 1 # Sleep (pause) for 1 second
showdate # Call itself
}
clear # Clear the screen
showdate # Call the function which will display the clock
If you execute this script it will run indefinitely until you hit CTRL-C
If you want to add the date to the terminal window title and your terminal supports it, you can do
#! /bin/bash
while :
do
printf '\033]0;%s\007' "$(date)"
sleep 1
done &
and it won't affect any other terminal output.
This post is ancient, but for anyone still looking for this kind of thing:
#!/bin/bash
printf '\033[;H'
while true; do date; sleep 1; done
This version avoids recursion (and stack faults!) while accomplishing the goal.
(For the record - I do prefer the watch version, but since the OP did not like that solution, I offered an improvement on the other solution.)

Sending Ctrl-L via dbus to terminal emulator

One can send text via dbus to the terminal emulator konsole as followed:
qdbus org.kde.konsole /Sessions/1 sendText "hello"
However I want to remotely clear the screen of the specified terminal window.
So I tried:
qdbus org.kde.konsole /Sessions/1 runCommand "clear"
Does partly what I want. Only problem: The screen doesn't get cleared when there is a process running.
In the terminal emulator, in this case the key combination "Ctrl + L" would do the job.
So I'm trying to send a string with escape characters for this shortcut.
Is this going to work? This, however doesn't do;
qdbus org.kde.konsole /Sessions/1 sendText "\033[2J"
(runCommand neither)
This is working for me:
qdbus org.kde.konsole /Sessions/1 sendText $'\014'
First, to produce a character from its octal code, the syntax "\033" would work in C but not in Bash.
Second, while "ESC [ 2 J" is a VT100 code to Erase Screen, it work for me only if I echo $'\033[2J', and that will not work if a command is running.
Third, Ctrl-L will work if a program is expecting input from a terminal (like irb or python do), but it will not work for a while sleep 1; do echo Still running; done loop.

Does tmux support relative line-numbers?

Background.
I am a frequent vim-user, I just love the way you can navigate the buffers without ever having to reach for the mouse. I'm especially fond of the relative line numbers that let's me jump to specific lines with perfect accurecy, it just makes navigating that much faster. I also use tmux quite a bit since I often have a lot of stuff going on in my terminal.
The thing that bugs me the most though is when I use tmux copy-mode, it just takes forever to navigate to the line(s) you want to copy if you are using the arrow-keys, ctrl+p or k.
Searching for a unique keyword in the buffer is also not ideal but it might be faster if you already know what to search for. A lot of the time you make a search only to discover that the keyword you searched wasn't so unique after all and you didn't end up on the line you wished for anyway.
My question is this:
Does tmux support relative line-numbers?
..or line-numbers at all for that matter?
I can't find any information about this on the web. Nobody seems to be mentioning anything about this anywhere. Is there a better way?
Any other tips for ultra-speedy navigation in tmux copy-mode using the keyboard would also be very much appreciated.
tmux has a linenumber system in copy mode. however the first line is very bottom line.
In copy mode you can press : to go to line but there is no option to show linenumber. You can use some vim motions (key-mode was set as vi) in copy-mode, e.g. j k 20j 20k f F t T gg G 20G H L M ^ $ / ? ctrl-u ctrl-d w b ....
I think for copy a block of text, it is enough.. If you think you still cannot "ultra-speedy navigation", make a scenario, let's see how could we copy faster.
check man-page of tmux for details.
I found this tip. It will take you to your line with less keystrokes.
# super fast way to reach copy-mode and search upwards
bind-key / copy-mode \; send-key ?
This is a total hack but it works:
tmux split-window -h -l 3 -b "printf '\e[38;5;0m\e[48;5;226m' \
&& seq 200 1 \
&& echo -n 0 \
&& read" \
&& tmux select-pane -l
(newlines added for readablitiy)
To break this down:
tmux split-window -h -l 3 "command..." splits the pane -h horizontally (that is places a new pane next to the current one rather than above or below) with a -l width of 3 (you're unlikely to need more than 3 digits of line number... 0-999) to the -b left of the current pane and runs the command in it:
printf ... just sets the background colour to yellow and the foreground colour to black... You can omit this bit if you're not feeling fancy :)
seq 200 1 prints line numbers from 200 to 1 - extend if you have a tall screen!
echo -n 0 prints the 0 on the last line, because seq will print a trailing newline and we don't want that
read waits for you to press enter - this is how we block it from closing after the echo has completed
tmux select-pane -l jumps you back to focus on the pane you were working on
Select the pane and press enter to close it.
I would imagine that you can do something add a name for the new pane and create a keybinding for both opening and closing it from the pane you're actually trying to count line numbers on, but for now I'm just using the binding:
bind N split-window -h -l 3 -b "printf '\e[38;5;0m\e[48;5;226m' && seq 200 1 && echo -n 0 && read" \; select-pane -l

Use backtick/Grave/Tilde as modifier key

I'm looking for a way to create keyboard shortcuts in Linux using backtick (`) / tilde (~) key and some other key. In an ideal case:
pressing tilde down does nothing
pressing another key while the tilde is pressed down triggers a (customizable) shortcut
when releasing the tilde before/without pressing another key, just send the tilde keystroke.
I have something similar in AutoHotKey for Windows, and i've been searching for a way to recreate this in a (any) Linux environment. I would consider using any GUI if i could get this working, but of course a more "general" solution would be even better.
I think i finally got it!!
I use xmodmap to turn the grave key into the modifier Hyper_L, and XCape to send the grave if the key is released without another key being pressed.
Xcape was intended to open the app menu ("Start menu") when the meta-key ("windows key") is pressed and released without another key, so as an added bonus, it does that too. Meaning you can both use Meta as a modifier, like Meta-F to open the file manager AND use the meta-key seperately to open the whiskers menu.
If all is right, you can open the keyboard settings manager using ~-k, and you can make new shortcuts using the ~-key. Because that's still tiresome and not easily portable between different systems, i've included some shortcuts using xfconf-query, which will probably only work in Xfce.
Here's the basics of my script:
#!/bin/sh
# reset pretty much ALL keyboard settings
setxkbmap
# Free up the mod3 and mod4 flags from all keys it may be associated with:
xmodmap -e "clear mod3"
xmodmap -e "clear mod4"
# Add Hyper_L to the grave key (49)
xmodmap -e "keycode 49 = Hyper_L asciitilde grave asciitilde"
# You need a grave key somewhere else (!) so, bind it to an unused key:
xmodmap -e "keycode 250 = grave"
# Restore Mod4 but without Hyper_L (which was at location 4)
xmodmap -e "add mod4 = Super_L Super_R Super_L"
# Assign the mod3 to Hyper_L:
xmodmap -e "add mod3 = Hyper_L"
dist=100
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Right -s "xdotool mousemove_relative -- $dist 0" --create -t string
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Down -s "xdotool mousemove_relative -- 0 $dist" --create -t string
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Left -s "xdotool mousemove_relative -- -$dist 0" --create -t string
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Up -s "xdotool mousemove_relative -- 0 -$dist" --create -t string
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>space -s "xdotool click 1" --create -t string
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>k -s "xfce4-keyboard-settings" --create -t string
# (re)starting xcape to produce a ` after key-up if no other key was pressed
killall xcape
xcape -t5000 -e "#49=grave;Super_L=Control_L|Escape" &
A more extended version of the script, with some more shortcuts can be found here.
I am not sure if it will work out for you but you should check:
xdotool (http://manpages.ubuntu.com/manpages/precise/man1/xdotool.1.html)
zenity (http://manpages.ubuntu.com/manpages/precise/man1/zenity.1.html)
Both these tools let you create some custom actions and shortcuts.
Here is an example of xdotool: https://askubuntu.com/questions/212154/create-a-custom-shortcut-that-types-clipboard-contents
Hope it helps and good luck :)
Bruno
After switching to Ubuntu on another machine, I also wanted to use tilde as a modifier key the way I use it in my AHK scripts.
I did quite some research on different tools for ex. xdotool, xev, autotools, xbindkeys and so on and finally found a solution to that. Here are the steps.
Install Autokey, python, python evded module, xte(sudo apt-get install xautomation).
Read a little about Autokey and how it launches python scripts or create hotstrings. In Autokey, we can set a hotkey to call a python script. So you can assign the python script down below to your tilde key or for that matter whatever custom hotkey you plan to create.
Here is the required custom functionality(thought not yet completely ported to linux, I have it scripted in autohotkey and just love it. It keeps the hand glued to keyboard ;))
Tilde + Up arrow: Move mouse pointer upwards by 100 positions
Tilde + Down arrow: Move mouse pointer downwards by 100 positions
Tilde + Right arrow: Move mouse pointer to right by 100 positions
Tilde + Left arrow: Move mouse pointer to left by 100 positions
Tilde + Enter: Left Mouse click (not present in the python script)
Tilde + Alt + Enter: Right Mouse Click
I use the tilde (KEY_GRAVE) as my modifier key. When this key is pressed, Autokey launches the python script. The scripts runs a loop until the tilde key is released. In the loop, the script keeps on detecting the keyboard inputs. On a UP arrow key(KEY_UP) press, the script sends a command to move a mouse by relative position (0, -100) utilizing 'xte' and so on.
from evdev import InputDevice, categorize, ecodes
from select import select
dev = InputDevice('/dev/input/event4')
releasekey = False
while releasekey==False:
r,w,x = select([dev], [], [])
for event in dev.read():
if event.type == ecodes.EV_KEY:
#system.exec_command("xte 'mousermove 0 3'", False)
#break
if event.code == ecodes.KEY_UP:
if event.value == 1:
system.exec_command("xte 'mousermove 0 -100'", False)
if event.code == ecodes.KEY_DOWN:
if event.value == 1:
system.exec_command("xte 'mousermove 0 100'", False)
if event.code == ecodes.KEY_RIGHT:
if event.value == 1:
system.exec_command("xte 'mousermove 100 0'", False)
if event.code == ecodes.KEY_LEFT:
if event.value == 1:
system.exec_command("xte 'mousermove -100 0'", False)
if event.code == ecodes.KEY_GRAVE:
if event.value == 0:
releasekey = True
break
You have to adjust the dev = InputDevice('/dev/input/event4') line to assign the correct name of your keyboard. In my case, event4 is my keyboard. Yours might be different. You can check out the handy tutorial "Reading Events" on python-evdev. That code actually outputs the name of your keyboard listed under /dev/input. Actually, my script is an extention to that tutorial script.
The only problem is that the python script must be started as a root user otherwise the keyboard input device can not be accessed. You can overcome this by creating a udev rule file which changes the permission of the device to make it available for reading writing eg. create a rule file and add this line
KERNEL=='event4', MODE="0660" and load the rule . At the end you must add yourself in the GROUP which have read/write permission for the device. The information regarding file permission can be found using ls -la in the /dev/input folder.
I hope it works for you. In it doesn´t work at first go, then get a cup of coffee and fight on till it works ;)

Resources