#!/usr/bin/perl

my $VERSION = '1.0';

my @keys = ('Up', 'Down', 'Left', 'Right');

open (XMODMAP, "xmodmap -pk |") or die "couldn't run xmodmap -pk: $!\n";

while (my $line = <XMODMAP>) {
	chomp($line);
	$line =~ s/^\s*(.*?)\s*$/$1/;
	my ($keycode, @values) = split(/[	 ]+/, $line);

	for my $index (0..$#values) {
		$values[$index] =~ s/^\((.*)\)$/$1/;
	}

	my @primary = (shift @values, shift @values);

	for my $key (@keys) {
		if ($primary[1] eq $key) {
			my @command = ('xmodmap', '-e', "keycode $keycode = $key");
			print join(', ', @command), "\n" if (@ARGV);
			system(@command);
		}
	}

}

