| 1 | package Tk::AppWindow::Ext::Keyboard; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Tk::AppWindow::Ext::Keyboard - adding easy keyboard bindings | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use vars qw($VERSION); | ||
| 12 | $VERSION="0.15"; | ||
| 13 | use Tk; | ||
| 14 | |||
| 15 | use base qw( Tk::AppWindow::BaseClasses::Extension ); | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | my $app = new Tk::AppWindow(@options, | ||
| 20 | -extensions => ['Keyboard'], | ||
| 21 | ); | ||
| 22 | $app->MainLoop; | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | =head1 CONFIG VARIABLES | ||
| 27 | |||
| 28 | =over 4 | ||
| 29 | |||
| 30 | =item Switch: B<-keyboardboardbindings> | ||
| 31 | |||
| 32 | Default value is an empty list. | ||
| 33 | |||
| 34 | Specify a paired list of keyboard bindings. | ||
| 35 | |||
| 36 | =back | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub new { | ||
| 41 | my $class = shift; | ||
| 42 | my $self = $class->SUPER::new(@_); | ||
| 43 | |||
| 44 | $self->addPreConfig( | ||
| 45 | -keyboardbindings => ['PASSIVE', undef, undef, []], | ||
| 46 | ); | ||
| 47 | |||
| 48 | $self->{BOUND} = {}; | ||
| 49 | $self->addPostConfig('ConfigureBindings', $self); | ||
| 50 | return $self; | ||
| 51 | } | ||
| 52 | |||
| 53 | =head1 METHODS | ||
| 54 | |||
| 55 | =over 4 | ||
| 56 | |||
| 57 | =item B<AddBinding>I<($command, $key)> | ||
| 58 | |||
| 59 | Adds a keyboard binding to the MainWindow object. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub AddBinding { | ||
| 64 | my ($self, $command, $key) = @_; | ||
| 65 | my $bound = $self->{BOUND}; | ||
| 66 | my $w = $self->GetAppWindow; | ||
| 67 | $key = $self->Convert2Tk($key); | ||
| 68 | unless (exists $bound->{$command}) { | ||
| 69 | $bound->{$command} = $key; | ||
| 70 | $w->bind("<$key>", [$w, 'cmdExecute', $command]); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | sub ConfigureBindings { | ||
| 75 | my $self = shift; | ||
| 76 | my $bindings = $self->configGet('-keyboardbindings'); | ||
| 77 | my @b = @$bindings; | ||
| 78 | while (@b) { | ||
| 79 | my $command = shift @b; | ||
| 80 | my $key = shift @b; | ||
| 81 | $self->AddBinding($command, $key); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | =item B<Convert2Tk>I<($key)> | ||
| 86 | |||
| 87 | Converts the modern description of a keyboard to to the Tk version. | ||
| 88 | For example, 'CTRL+C' becomes 'Control-c'. | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub Convert2Tk { | ||
| 93 | my ($self, $dkey) = @_; | ||
| 94 | my $shift = 0; | ||
| 95 | my $ctrl = 0; | ||
| 96 | my $alt = 0; | ||
| 97 | while ($dkey =~ s/^([^\+]+)\+//) { | ||
| 98 | if ($1 eq 'SHIFT') { | ||
| 99 | $shift = 1; | ||
| 100 | } elsif ($1 eq 'CTRL') { | ||
| 101 | $ctrl = 1; | ||
| 102 | } elsif ($1 eq 'ALT') { | ||
| 103 | $alt = 1; | ||
| 104 | } else { | ||
| 105 | die "Unrecognized key $1 in key conversion"; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | if ((length($dkey) eq 1) and ($dkey ge 'A') and ($dkey le 'Z')) { | ||
| 109 | $dkey = lc($dkey) unless $shift; | ||
| 110 | $shift = 0; | ||
| 111 | } | ||
| 112 | my $tkkey = ''; | ||
| 113 | $tkkey = 'Control-' if $ctrl; | ||
| 114 | $tkkey = $tkkey . 'Alt-' if $alt; | ||
| 115 | $tkkey = $tkkey . 'Shift-' if $shift; | ||
| 116 | $tkkey = $tkkey . $dkey; | ||
| 117 | return $tkkey | ||
| 118 | } | ||
| 119 | |||
| 120 | sub ReConfigure { | ||
| 121 | my $self = shift; | ||
| 122 | my $bound = $self->{BOUND}; | ||
| 123 | my $w = $self->GetAppWindow; | ||
| 124 | for (keys %$bound) { | ||
| 125 | my $key = delete $bound->{$_}; | ||
| 126 | $w->bind("<$key>", ''); | ||
| 127 | } | ||
| 128 | $self->ConfigureBindings | ||
| 129 | } | ||
| 130 | |||
| 131 | =back | ||
| 132 | |||
| 133 | =head1 AUTHOR | ||
| 134 | |||
| 135 | Hans Jeuken (hanje at cpan dot org) | ||
| 136 | |||
| 137 | =head1 BUGS | ||
| 138 | |||
| 139 | Unknown. If you find any, please contact the author. | ||
| 140 | |||
| 141 | =head1 SEE ALSO | ||
| 142 | |||
| 143 | =over 4 | ||
| 144 | |||
| 145 | =item L<Tk::AppWindow> | ||
| 146 | |||
| 147 | =item L<Tk::AppWindow::BaseClasses::Extension> | ||
| 148 | |||
| 149 | =back | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | 1; | ||
| 154 | |||
| 155 |