aboutsummaryrefslogtreecommitdiffstats
path: root/history-search/key_test.pl
blob: b16ff00229daca723ee5452068730d82248c739b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use strict;
use Irssi;
use Irssi::TextUI; # for sbar_items_redraw

use vars qw($VERSION %IRSSI);
$VERSION = "1.0.1";
%IRSSI = (
	authors         => "shabble",
	contact         => 'shabble+irssi@metavore.org, shabble@#irssi/Freenode',
	name            => "",
	description     => "",
	license         => "Public Domain",
	changed         => ""
);


Irssi::signal_add_last 'gui key pressed' => \&got_key;

my $buf = '';

sub got_key {
    my ($key) = @_;
    $buf .= " $key";
    my $res = decode_keypress($key);
    if (defined $res) {
        print "codes: $buf";
        print "Keypress: $res";
        $buf = '';
    }
}

# 1 means we've seen an esc, 2 means we've
# seen an esc,[. 0 is normal.

my $decoder_state = 0;

sub decode_keypress {
    my ($code) = @_;

    if ($decoder_state == 1) {

        # seen esc/meta.
        if ($code == ord('[')) {
            $decoder_state = 2;
            return undef;
        } else {
            $decoder_state = 0;
            return 'meta-' . chr($code);
        }

    } elsif ($decoder_state == 2) {

        if ($code == ord('A')) {
            $decoder_state = 0;
            return 'up-arrow';
        } elsif ($code == ord('B')) {
            $decoder_state = 0;
            return 'dn-arrow'
        } elsif ($code == ord('C')) {
            $decoder_state = 0;
            return 'right-arrow'
        } elsif ($code == ord('D')) {
            $decoder_state = 0;
            return 'left-arrow'
        }

        $decoder_state = 0;
        return undef;

    } else {

        if ($code < 27) {

            if ($code == 9) {
                return 'tab';
            }

            return 'ctrl-' . chr($code + ord('a') -1);
        }

        if ($code > 32 && $code < 127) {
            return chr($code);
        }

        if ($code == 32) {
            return 'space';
        }

        if ($code == 127) {
            return 'backspace';
        }

        if ($code == 27) {
            $decoder_state = 1;
            return undef;
        }

        return 'unknown ' . $code;
    }
}