Sunday, September 20, 2009

Using Java Robot to type text strings

The Java Robot class is very useful for automating various tasks that normally require user interaction. However, it lacks a rather simple but useful feature - it cannot type a string of text, just a single key code. I had to implement this when I worked on one of my projects and to my surprise an extensive googling session showed that quite a lot of people are looking for such functionality.

The SmartRobot class below extends the standard Java Robot class adding to "type(String text)" and a few other useful methods:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class SmartRobot extends Robot {

public SmartRobot() throws AWTException {
super();
}

public void keyType(int keyCode) {
keyPress(keyCode);
delay(50);
keyRelease(keyCode);
}

public void keyType(int keyCode, int keyCodeModifier) {
keyPress(keyCodeModifier);
keyPress(keyCode);
delay(50);
keyRelease(keyCode);
keyRelease(keyCodeModifier);
}


public void type(String text) {
String textUpper = text.toUpperCase();

for (int i=0; i<text.length(); ++i) {
typeChar(textUpper.charAt(i));
}
}

private void typeChar(char c) {
boolean shift = true;
int keyCode;

switch (c) {
case '~':
keyCode = (int)'`';
break;
case '!':
keyCode = (int)'1';
break;
case '@':
keyCode = (int)'2';
break;
case '#':
keyCode = (int)'3';
break;
case '$':
keyCode = (int)'4';
break;
case '%':
keyCode = (int)'5';
break;
case '^':
keyCode = (int)'6';
break;
case '&':
keyCode = (int)'7';
break;
case '*':
keyCode = (int)'8';
break;
case '(':
keyCode = (int)'9';
break;
case ')':
keyCode = (int)'0';
break;
case ':':
keyCode = (int)';';
break;
case '_':
keyCode = (int)'-';
break;
case '+':
keyCode = (int)'=';
break;
case '|':
keyCode = (int)'\\';
break;
// case '"':

// keyCode = (int)'\'';

// break;

case '?':
keyCode = (int)'/';
break;
case '{':
keyCode = (int)'[';
break;
case '}':
keyCode = (int)']';
break;
case '<':
keyCode = (int)',';
break;
case '>':
keyCode = (int)'.';
break;
default:
keyCode = (int)c;
shift = false;
}
if (shift)
keyType(keyCode, KeyEvent.VK_SHIFT);
else
keyType(keyCode);
}

private int charToKeyCode(char c) {
switch (c) {
case ':':
return ';';
}
return (int)c;
}
}


The code can be downloaded here

Labels: , , , , , ,

Wednesday, October 15, 2008

Hebrew for Motorola RAZR2 V8

RAZR2 V8 is not sold officially in Israel and according to Motorola it has no Hebrew support. Fortunately, as it often happens, they (Motorola) don't really now what they are talking about. Although there is no LP0033 (Language Pack 0033) which usually includes Hebrew support, LP0034A which can be downloaded here includes, among other languages, Hebrew fonts!

Enjoy.

Update:
It appears that LP0034A does not include full Hebrew language support, just the Hebrew fonts (which is enough to read Hebrew text messages).

Labels: , , ,

Saturday, September 06, 2008

Never use DriveImage XML for backup

DriveImage XML looks very nice - it's free, has a simple and easy-to-use menu, supports all the features that you would normally need to backup your system:

  • Can backup drive that it (and Windows) is running from
  • Available as Bart PE module and can be easily integrated into Bart PE bootable CD or USB flash drive
  • And more


It's a charming piece of software, until you actually have to use it to restore you backup. Then you suddenly discover that it is almost impossible to restore you backup!

First, it does not really support multiple CD/DVD media. This is especially frustrating, as during backup it creates multiple image chunks of 650MB size, which makes you think that it will be able to restore the drive from multiple CD media. Bummer! When you try to do so, you get “error reading from Compressed stream” message.

Second, it can restore to existing partition only, however during the restore process it destroys that partition and if the restore fails - you have to recreate this partition using some other software.

And, when and if you somehow manage to restore the image you discover that it's not bootable. But this is easy to fix with fdisk (just make the restore partition active), provided you have a bootable CD/USB with relevant utilities.

To summarize, if your backup does not fit into a single DVD media, you can only use DriveImage XML to restore from network drive (or physically connect you faulty drive to another PC).

OR better yet, use a different backup software.

Actually, this is an interesting marketing strategy - most people use backup software to backup only, restores are rare. So it pays off to invest in backup features and neglect almost entirely restore functionality.

Labels: , ,

Wednesday, September 03, 2008

How to boot Windows XP from USB flash drive

This stuff is not exactly new, but nevertheless quite useful, as it appears that there are lots of contradictory and quite complex guides on the subject.

The truth is that it is quite simple to make bootable WinXP USB drive (at least it worked like a charm for me and it would be interesting to learn about other people's experiences on different hardware).

Prerequisite:


Instructions:

  • Download and install BartPE bulder and PeToUSB
  • Run BartPE, follow the instructions to create Windows PE installation
  • Run PeToUSB, follow the instructions to format the USB drive and copy Windows PE files


That's it!

Labels: , , , , ,

Wednesday, May 21, 2008

Upgrading Fedora i386 to x86_64

Fedora Core 9 installation script warns that upgrading FC i386 to FC9 x86_64 is likely to fail, which is indeed what happens if you chose to proceed. However, the resulting mess can be fixed relatively easily.

First, the grub configuration must be fixed. Just boot from the installation DVD, do "chroot /mnt/sysimage", edit grub.conf file to include the correct kernel and other filenames (note that for some reason it was moved from /etc/grub.conf to /boot/grub/grub.conf) and rerun grub-install. After that you should be able to boot.

The next step is to fix yum/rpm configuration. Edit "/etc/rpm/platform" and change the platform to "x86_64-redhat-linux". After that yum should work OK, except that for some reason the DVD repository may not be configured correctly. Check that " /etc/yum.repos.d/Fedora-install-media.repo" (or any other InstallMedia configuration file) has the following:

[InstallMedia]
name=Fedora 9
mediaid=1210111941.792844
metadata_expire=-1
gpgcheck=0
cost=500
enabled = 1
baseurl=file:///media/Fedora%209%20x86_64%20DVD


Now you should have more or less working system with lots of packages from previous installation which were not upgraded correctly. I wrote a small script which looks for all Fedora Core 8 packages and upgrades them to FC9 x86_64. Remember to disable all repositories except for InstallMedia before running the script:

for i in `rpm -qa | grep fc8 `; do
echo $i
j=`rpm -q $i --qf %{NAME}`
echo $j
rpm -e --nodeps $i
yum -y install $j
done


You may also want to upgrade all "i386" packages in the same way, in which case just replace "fc8" with "i386" in the above script.

Labels: , , , , , ,

Thursday, November 29, 2007

Embedded Linux development tools

I read a number of articles about embedded Linux development tools. Some claimed that these tools do not exist, yet another one tried to convince everybody that embedded Linux tools do not exist because there is no market for them.

My recent article at Embedded Systems Design magazine briefly mentions a few commercial offerings (yes, these tools exist and the companies that produce them are profitable), but it concentrates mostly on what you can do (hint - quite a lot) if you chose the do-it-yourself approach to embedded Linux tools.

Friday, November 02, 2007

aMule restart script

Accidentally discovered that some people still use my old aMule watchdog script. The script can be downloaded here. It is quite trivial - it simply checks from time to time that somebody is still listening on the aMule port. Since aMule tends to crash and freeze quite often (at least used to when I was using it) it is rather handy.

The script:

#!/bin/sh

# aMule watchdog
# Checks on aMule every 5 (configurable parameter) minutes
# and restarts it if aMule does not respond.
# Requires Netcat
#
# Created by Demiurg - sasha.sirotkin [AT] gmail.com
# Have fun.

MULENAME=amule
MULE=`which ${MULENAME}`
NC=`which nc`

TCPPORT=4642
SLEEP=300

test -e ${MULE} || (echo "${MULE} not found. Dying" ; exit 1)
test -e ${NC} || (echo "Netcat not found. Dying" ; exit 1)

FIRSTLOOP=1

rm ~/.aMule/muleLock

while true; do
# echo "Waiting"
test ${FIRSTLOOP} || sleep ${SLEEP}
unset FIRSTLOOP
# echo "Checking"
nc localhost ${TCPPORT} < /dev/null && continue
echo "Restarting"
date
killall -9 ${MULENAME}
sleep 1
${MULE} &
# echo "Running"
done

Labels: , , ,