As of 2016-02-26, there will be no more posts for this blog. s/blog/pba/
Showing posts with label keyboard. Show all posts

Last week, We Wish You a Merry Christmas was playing somewhere, an idea came to me, what if your computer sings one note on every key you press? That would be fun, wouldn’t it?

And here is the merryxmas.sh and a video to demonstrate.

https://i.ytimg.com/vi/D1gXN7wZ7IM/maxresdefault.jpg

1   About the code

I decided to write it entirely in Bash with some tools, such as xinput and play from Sox.

I could have forked the xinput to modify and strip it for this purpose, it would be cleaner since I really don’t have any idea how to handle the buffer issue1. But with shell scripting, it’s easier to modify if there are suitable tools.

Originally, I was going for only keyboard presses, but then I added mouse clicks, and then the movements. It turned out pretty nice.

As for xinput and its test, unfortunately, it can only monitor one device and you also need to specify the device name. I needed to get both keyboard and mouse events in altogether at the same time, I tried to use mkfifo, but that didn’t work, so I resolved with regular files and those would increase in file size.

Its test-xi2 works for all input devices, but it pops up a window like xev and that wouldn’t be a good surprise for your family and friends, right?

The choice of sound playing is play from Sox, which I don’t believe many computer would have it, but at least it’s included in almost all distributions’ package managers. I could go with wave, but you would have to download and compile it. But it might be a better option, because I doubt anyone would have your target’s computer’s password to gain root access for using package manager to install Sox

[1]As you are required to run with stdbuf, without it, due to the buffer size, you will have to press a lot to get the song singing.

2   Some ideas

This script is just a basic one. I could think of a few more that you can wish your family and friends:

  1. Run Xsnow while or after playing.
  2. Pop up a message box or using xdotool2 to type in the message.
  3. Only play a note when hitting one of “xmas” or “christmas” keys.
  4. Play the whole song at once if “xmas” or “christmas” is exactly spelled.

I personally, like 1 and 4, especially 4, because during this holiday season, you can be sure the chance that a user would type the word in is very high, almost unavoidable. They can be writing an email to family and friends, just as typing in the subject line, the computer sings. Wouldn’t that be fun?

[2]I use xdotool to enter timestamp.

It always is a wonderful experience when you witness an evolution. Since the invention of typewriter, only low-skilled users would require 10 fingers to operate on a keyboard. Until today, it remains perfectly true.

As technology progresses, computer entered our daily life and has been dominating every corner in the entire world since then. At the beginning of commercial computing, only the people like secretaries, data entry, or code monkeys would require having no skill of the 10-finger typing whilst bosses only use 1 finger or 2 fingers to type in.

Using 1 finger or 2 fingers are the hardest way to type, because, firstly, you need to clear the field. You need a clear view of keyboard, so you can locate the key you are about to press. Imagining that there are around 100 keys on the board, you need to spot them as fast as you can. And for aged keyboard, the printing would have worn out, you may have to guess which one is correct key, this only demonstrate how skillful of 1-or-2-finger typer is.

After a desired key is spotted, the typer's motor skill kicks in, he or she presses the key with quick and precise press, then immediately retreats from the keyboard.

At this moment, the task hasn't been completed, far from it. The typer must change the focus from keyboard to monitor, the typer's eyes have to adapt such dramatic change in a short time. Seeking for the new typed letter on the screen where the cursor at which typer has to remember before typing.

Once confirmed, then repeat the process. As you can see, 1-or-2-finger is more skillful than 10-finger typer, who only stares at the screen all the time and type like nothing, there is no skill in them.

After the mouse device invented, till today, it's basically a three-button pointer device. Some 1-or-2-finger typers evolve into more skillful, then now may only use only one hand to control the whole computer, which is the same hand control the mouse.

Using a mouse is even harder then typing. The main goal is to find the Next button on the screen, which isn't always on the screen or it could be hard to be seen. Often times, the mouse users have to combine different types of controlling skills, suck as double-click, right-click, drag-and-drop, etc. On keyboard, it's only one, press.

Later, the mobile devices has become more important for one's life. Screen gets smaller as well as the keyboard, physical or virtual, the typer's skill is forced to evolve once more.

Then, the groundbreaking device was invented, the mighty iPhone and later iPad. Typer is no longer a typer, but a toucher. With only one finger to operate, as you can guess, it requires smarter people to use such devices. They have to learn new gestures in order to operate, such as slide. Fortunately, multi-touch is added later for people less skilled, who used to 10-finger typing.

Not only the devices evolve, but also the web. Less than a year ago, Google began a process to unify its products' designs starting with its navigation bar, Gmail, Google Groups, Google Reader, and more has already adopted. Things got bigger, such as buttons, that's good for toucher to touch. The looks is more like designs on *pad, like a millions of websites have already imitated *pad design. A more recent example is the Google+: Toward a simpler, more beautiful Google.

Google was wrong about it, it never was simpler. Such design requires users' long-trained skills. For users who have trained themselves from 10-finger typing to 1-finger touching, then it is simpler. But for newcomers, it requires a long period of training. Unfortunately, for less skilled 10-finger typer, the old aged and stubborned users who just won't die, but there is no resolution for them.

In foreseeable future, human may no longer need any fingers to operate a computer. However, it requires more skillful person to operate without a finger as it already is proved with decades of computing history as described above.

I recently found out you can use xinput to enable or disable an input device. I used to use synclient to disable my touchpad, or using this tricky way to disable my laptop keyboard.

The first step to get device name or id of the device:


% xinput list
⎡ Virtual core pointer id=2 [master pointer (3)]
⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
⎜ ↳ USB Optical Mouse id=8 [slave pointer (2)]
⎜ ↳ SynPS/2 Synaptics TouchPad id=7 [slave pointer (2)]
⎣ Virtual core keyboard id=3 [master keyboard (2)]
↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
↳ Sleep Button id=9 [slave keyboard (3)]
↳ Power Button id=10 [slave keyboard (3)]
↳ Video Bus id=11 [slave keyboard (3)]
↳ AT Translated Set 2 keyboard id=6 [slave keyboard (3)]

For touchpad, the device name is 'SynPS/2 Synaptics TouchPad' and id is 7; for keyboard, they are 'AT Translated Set 2 keyboard' and 6. Next step is to know the properties of a device:


% xinput list-props 'AT Translated Set 2 keyboard'
Device 'AT Translated Set 2 keyboard':
Device Enabled (127): 1

This keyboard only has a property 'Device Enabled' whose value is 1, that means this keyboard is enabled. To test disabling:


sleep 0.1 ; xinput set-prop 'AT Translated Set 2 keyboard' 'Device Enabled' 0 ; sleep 5 ; xinput set-prop 'AT Translated Set 2 keyboard' 'Device Enabled' 1

The first sleep 0.1 is to prevent enter keypress being repeatedly sent somehow when you directly disable the keyboard, I am guessing when you hit the enter and the command is executed, meaning the keyboard is disabled, but the keyup event is not yet sent, so X still thinks the enter key is pressed down.

Another simple way is to use id, so you don’t need long device name:


sleep 0.1 ; xinput set-prop 8 127 0 ; sleep 5 ; xinput set-prop 8 127 1

8 is id of this keyboard and 127 is the property id of 'Device Enabled'. When you list properties of device using list-props, the numbers after property names are the property ids. It seems that 'Device Enabled' always has id 127, but device id is not always the same. It depends on device attached time, who shows up first who gets next available id.

One more thing to note: I don’t need root privilege to set property value.