Shortkeys.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Shortkeys/Manager.pm ↑ Summary
1 package Win32::Shortkeys;
2 #use lib qw( U:/docs/perl/mod/hg_Win32-Shortkeys-Kbh/lib);
3
4 =head1 NAME
5
6 Win32::Shortkeys - A shortkeys perl script for windows
7
8 =cut
9
10 our $VERSION = '0.05';
11
12 =head1 VERSION
13
14 0.05
15
16 =cut
17
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Config::YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 0 ▶use Win32::Clipboard;
Subroutine BEGIN: called CC=1 SCAR=2.6
22 use Win32::Shortkeys::Kbh qw(:all);
23 use Win32::Shortkeys::Manager;
24 use XML::Parser;
25 #use Data::Dumper;
26 use Time::HiRes qw(usleep);
27 use Carp;
28 use Encode;
29
30 #my %data;
31
32 my %shk_use_clpbrd;
33
34 sub new {
35 my ( $class, $file ) = @_;
36
37 my $self = bless( {}, ref($class) || $class );
38 my $usage = <<END;
39 usage Win32::Shortkeys->new(config_file);
40 END
41 die $usage unless ($file);
42
43 $self->{config} = Config::YAML::Tiny->new( config => $file );
44 return $self;
45 }
46
47 sub run {
48 my $self = shift;
49 my $com_map = $self->{config}->get_vkcode_map
50 or confess("vkcode_map undefined");
51
52 for my $k ( keys %$com_map ) {
53
54 # print "$k: ", eval $com_map->{$k}, "\n";
55 $com_map->{$k} = eval $com_map->{$k} or confess($@);
56
57 }
58
59 #die eval $self->{config}->get_quit_key;
60 $self->{quit_key} = eval $self->{config}->get_quit_key or confess($@);
61 $self->{load_key} = eval $self->{config}->get_load_key or confess($@);
62 $self->{usleep_delay} = eval $self->{config}->get_usleep_delay
63 or confess($@);
64 $self->{com_map} = $com_map;
65 my $xml = $self->parse_file;
66 $self->{shkm} = Win32::Shortkeys::Manager->new( $xml );
67
68 #$self->{shkm}->print_all;
69
70 set_key_processor( sub { $self->process_key(@_); } );
71
72 #set_key_processor(sub { $self->test(@_);});
73
74 register_hook();
75
76 msg_loop();
77
78 }
79
80 sub parse_file {
81 my $self = shift;
82 my $encoding = $self->{config}->get_file_encoding;
83 $encoding = ( $encoding ? $encoding : "UTF-8" );
84 my $path = $self->{config}->get_file_path
85 or confess("path to shortkeys xml file undefined");
86
87 # "<:raw:encoding($encoding):crlf:utf8",
88 # open( my $FH, "<:encoding($encoding)", $path ) or die "can't open file: $!";
89
90 #binmode(STDOUT, ":encoding(utf8)");
91 my $p =
92 XML::Parser->new( ErrorContext => 2, ProtocolEncoding => $encoding );
93
94 # 'Default' => \&MySubs::def,
95 # 'Final' => \&MySubs::final
96
97 $p->setHandlers(
98 'Start' => \&Win32::Shortkeys::MySubs::start,
99 'Char' => \&Win32::Shortkeys::MySubs::char,
100 'End' => \&Win32::Shortkeys::MySubs::end,
101
102
103 # 'Default' => \&MySubs::def
104 );
105
106 #print "parsing\n";
107 eval {$p->parsefile($path); };
108 if ( $@ ){
109 $@ =~ s/at \/.*?$//s; # remove module line number
110 print "\nERROR in '$path':\n$@\n";
111
112 } else {
113 print "'$path' parsed with success\n";
114
115 }
116 return Win32::Shortkeys::MySubs::get_data();
117
118 }
119
120
121
122 sub process_key {
123 my ( $self, $cup, $code, $alt, $ext ) = @_;
124
125 return unless $cup; #process key released, not key pressed
126 # print "process_key : $code\n";
127
128 if ( $code == $self->{quit_key} ) {
129 unregister_hook();
130 #Win32::Process::KillProcess( $$, -1 );
131 quit();
132
133 }
134 elsif ( $code == $self->{load_key} ) {
135 #%data = ();
136 my $xml = $self->parse_file;
137 $self->{shkm} = Win32::Shortkeys::Manager->new( $xml );
138 #$self->{shkm}->print_all;
139
140 }
141 else {
142 # usleep($self->{usleep_delay});
143 $self->{shkm}->listen($code);
144 }
145
146 if ( $self->{shkm}->is_ready ) {
147 my $shk = $self->{shkm}->get_shortkey;
148 unregister_hook();
149 if ( $shk_use_clpbrd{$shk} ) {
150 my $data = $self->{shkm}->get_data;
151 $data =~ s/\n/\015\012/g;
152 #utf8 required:
153 #my $oct = Encode::encode("cp1250", $data);
154 my $oct = Encode::encode("iso-8859-1", $data);
155 Win32::Clipboard::Set($oct);
156 #Win32::Clipboard::Set($data);
157 usleep( $self->{usleep_delay} );
158 # send length($shk) + 1 delete keys + ctrl + v
159 paste_from_clpb( length($shk) + 1 );
160 }
161 else {
162 my $rawdata = $self->{shkm}->get_data;
163
164 # send_string("Key hitted " . chr ( $code ));
165 $self->parse_raw_data( $rawdata, $shk );
166
167 }
168 register_hook();
169 # print ("ERROR in register_hook $@\n") if ($@);
170
171 }
172 }
173
174 sub parse_raw_data {
175 my ( $self, $raw, $shk ) = @_;
176 my @chunks = split( /#/, $raw );
177 $chunks[0] = $raw unless (@chunks); #do the loop below even if $raw is a zero length string
178 my $delkeys = length($shk) + 1;
179 my $pos = 0;
180 my $last = @chunks;
181 my %seen;
182 my %chunk_seen;
183 #print "last: $last\n";
184 #print "chunks: ", join( "*", @chunks ), "\n";
185
186 # my ($com, $text, $has_next, $next, $how_much);
187 for my $raw (@chunks) {
188 $chunk_seen{ $pos++ } = 0;
189 }
190 $pos = 0;
191 for my $raw (@chunks) {
192 my $com = undef;
193 my $text = undef;
194 my $has_next = ( $pos + 1 < $last ? 1 : 0 );
195 my $next = ( $has_next ? $chunks[ $pos + 1 ] : undef );
196 my $how_much = 1;
197 my $seen = $pos;
198 #print "raw: ", ( defined $raw ? $raw : " undef " ), " has_next: ", $has_next,
199 # " pos: ", $pos, " next: ", ( $next ? $next : " undef" ), "\n";
200 if ( $pos == 0 ) {
201
202 if ( !$raw && $has_next ) {
203 if ( !$next ) {
204 #print "jump over *", $next, "*\n";
205 next;
206 }
207 }
208 else {
209 # print "*** $raw $pos\n";
210 $text = $raw;
211 }
212 }
213 else {
214 #si l'élément en cours est vide et que le suivant existe
215 #la chaine contenait ##
216
217 if ( !$raw && $has_next ) {
218 $text = "#";
219 if ( $next && $has_next ) {
220 $text = "#" . $next;
221 }
222 $seen = $pos + 1;
223 }
224 else
225 { #sinon c'est une commande éventuellement suivie par du texte
226 $how_much = substr( $raw, 1, 2 );
227 $com = substr( $raw, 0, 1 );
228 if ( length($raw) > 3 ) {
229 $text = substr( $raw, 3 );
230 }
231 }
232 }
233 next if $chunk_seen{$seen};
234 if ($com) {
235 if ($delkeys) {
236 usleep( $self->{usleep_delay} );
237 send_cmd( $delkeys, VK_BACK );
238 }
239
240 #$com doit etre traduit par evmap \t
241 if ( exists $self->{com_map}->{$com}) {
242 my $vkcode = $self->{com_map}->{$com};
243 send_cmd( $how_much, $vkcode );
244 } elsif ($com eq "z"){
245 usleep ( $how_much * 100_000 );
246 } else {
247 carp ("undefined command abreviation: ", $com);
248 }
249 }
250 if (defined $text) { # send the delkeys even is text is a zero length string
251 if ($delkeys) {
252 usleep( $self->{usleep_delay} );
253 send_cmd( $delkeys, VK_BACK );
254 }
255 send_string($text);
256 }
257 $chunk_seen{$seen} = 1;
258 $delkeys = 0;
259 } #for
260 continue { $pos++; }
261 }
262
263 sub parse_raw_data_old {
264 my ( $self, $raw, $shk ) = @_;
265 my @chunks = split( /#/, $raw );
266
267 #my $text;
268 my $delkeys = length($shk) + 1;
269
270 #my $pos = 0;
271 #my $com = undef;
272
273 my $last = @chunks;
274 #print "last: $last\n";
275 #print "chunks: ", join( "*", @chunks ), "\n";
276 my %com_map = %{ $self->{com_map} };
277
278 #for $raw (@chunks) {
279 for ( my $pos = 0; $pos < $last; $pos++ ) {
280
281 #my $raw= $chunks[$pos];
282 my $com = undef;
283 my $text = undef;
284 my $how_much = 1;
285 print "raw: ", ( $chunks[$pos] ? $chunks[$pos] : " undef " ),
286 " pos: $pos\n";
287
288 #print "defined ", ( defined $chunks[$pos] ? $chunks[$pos] : " undef "), "\n";
289 #print "length ", (length $chunks[$pos] ? $chunks[$pos] : " length : 0 "), "\n";
290 if ( $pos == 0 ) {
291 if ( !$chunks[$pos] && ( $pos + 1 < $last ) ) {
292
293 #die "here";
294 if ( !$chunks[ $pos + 1 ] ) {
295 print "jump over *", $chunks[ $pos + 1 ], "*\n";
296
297 #$pos++;
298 next;
299 }
300 }
301 else {
302 $text = $chunks[$pos];
303
304 }
305
306 }
307 else {
308 #si l'élément en cours est vide et que le suivant existe
309 #la chaine contenait ##
310 if ( !$chunks[$pos] && ( $pos + 1 < $last ) ) {
311 $text = "#";
312
313 # (copies.length>i+1 && copies[i+1].length()>0)
314 if ( $chunks[ $pos + 1 ] && ( $pos + 1 < $last ) ) {
315 $text = "#" . $chunks[ $pos + 1 ];
316
317 }
318
319 #print "jumpover *", $chunks[$pos + 1], "*\n";
320 $pos++;
321
322 #next;
323 }
324 else
325 { #sinon c'est une commande éventuellement suivie par du texte
326 $how_much = substr( $chunks[$pos], 1, 2 );
327 $com = substr( $chunks[$pos], 0, 1 );
328 if ( length( $chunks[$pos] ) > 3 ) {
329 $text = substr( $chunks[$pos], 3 );
330 }
331 }
332 }
333 # print "text: ", ( $text ? $text : " undef" ), "\n";
334 # print "com: ", ( $com ? $com : " undef" ), "\n";
335 if ($com) {
336 if ($delkeys) {
337 usleep( $self->{usleep_delay} );
338 send_cmd( $delkeys, VK_BACK );
339 $delkeys = 0;
340 }
341
342 #$com doit etre traduit par evmap \t
343 my $vkcode = $com_map{$com};
344
345 #die $com;
346 send_cmd( $how_much, $vkcode );
347
348 }
349 if ($text) {
350 # print "delkeys: $delkeys\n";
351 if ($delkeys) {
352 usleep( $self->{usleep_delay} );
353
354 #die $delkeys;
355 send_cmd( $delkeys, VK_BACK );
356 }
357
358 #usleep($self->{usleep_delay});
359 send_string($text);
360 $delkeys = 0;
361 }
362
363 }
364 }
365
366 package Win32::Shortkeys::MySubs;
367 #use Data::Dumper;
368
369 my $shk;
370 my $current_text;
371 my %data;
372 sub start {
373 my ( $p, $el, %atts ) = @_;
374
375 my $key;
376 if ( $el eq "data" ) {
377 $shk = $atts{k};
378 $shk_use_clpbrd{$shk} = 0;
379 $current_text = undef;
380 if ( $atts{"use.ctrl_v"} ) {
381
382 # push @clpelems, $shk;
383 $shk_use_clpbrd{$shk} = 1;
384 }
385
386 }
387 elsif ( $el eq "dataref" ) {
388 $key = $atts{"id"};
389 $current_text .= $data{$key} if ( $data{$key} );
390
391 }
392
393 }
394
395
396 sub end {
397 my ( $p, $el ) = @_;
398 #if ( $current_text && $el eq "data" ) {
399 if ($el eq "data") {
400 # print "end\nshk : $shk : $current_text\n";
401 $data{$shk} = ( defined $current_text ? $current_text : "");
402 # if ( $shk eq "a" ) { print( "end : ", $current_text, "\n" ); }
403 $current_text = undef;
404
405 }
406
407 }
408
409 sub char {
410 my ( $p, $s ) = @_;
411 return unless $shk;
412 # $s =~ s/^[\f\t ]+//; # Replace leading tab with nothing
413 # $s =~ s/[\f\t ]+$//; #don't substitute in order to preserve space between datarefs elements
414 $s =~ s/[\f\t ]+/ /g;
415 $s =~ s/^\n$//g unless ( $shk_use_clpbrd{$shk} );
416
417 #print("char: $s L:", length($s), "\n");
418 $current_text .= $s if ( defined $s );
419
420
421 }
422
423 sub get_data{
424 return \%data;
425 }
426
427
428 =head1 SYNOPSIS
429
430 use Win32::Shortkeys;
431 my $s = Win32::Shortkeys->new("kbhook.properties");
432 $s->run;
433
434 Depending on the the sorkeys.xml file, some keystroke are replaced with string or keys command (enter, tab, cursor right ...) taken from this file.
435
436 =head1 DESCRIPTION
437
438 Since the synopsis above is short, the main things to describe are in the file pass to C<Win32::Shortkeys->new($file)>.
439
440 =head2 Properties file
441
442 It must follow the Config::YAML::Tiny syntax. Mine looks like
443
444 file_path: shortkeys_utf8.xml
445 file_encoding: UTF-8
446 use_ctrl_v: 1
447 load_key: VK_HOME
448 quit_key: VK_F12
449 usleep_delay: 400_000
450 vkcode_map:
451 t: VK_TAB
452 e: VK_RETURN
453 d: VK_DOWN
454 l: VK_LEFT
455 r: VK_RIGHT
456 x: VK_BACK
457 s: VK_SHIFT
458 c: VK_CONTROL
459 a: VK_MENU
460 w: VK_SPACE
461 h: VK_HOM
462
463 The key given in the load_key property is used to reload the shorkeys.xml file (without exiting the script).
464 The key given in the quit_key property is used to terminate the script.
465
466 =head2 The xml file
467
468 It's name is given by the C<file_path> property.
469 It's xml syntax is:
470
471 <?xml version="1.0" encoding="UTF-8"?>
472 <!DOCTYPE shortkey SYSTEM "dtd/shk.dtd">
473 <shortkey>
474 <data k='t'>Recent advances in biochemical and molecular diagnostics for the rapid detection of antibiotic-resistant Enterobacteriaceae: a focus
475 </data>
476 <data k='j'>Expert Review of Molecular Diagnostics
477 </data>
478 ....
479
480 </shortkey>
481
482 The values of the k attribute are a-z string composed of lower case character(s) (a string can have two or more characters).
483 I call those strings shortkeys and when press on the keyboard after they < key with the script running, the key pressed are replaced by the content of the corresponding data element.
484
485 For example, with the cursor in an opened notepad file, hitting the two keys <j when the script is running will replace this
486 two characters with the value of the corresponding <data> element: Expert Review of Molecular Diagnostics.
487
488 The shortkeys.xml file should be utf-8 encoded, even if the encoding can be defined in the properties.
489
490 With the key <, the script enter a "search mode" for a shortkey sequence. This key is hard coded and can't be changed (unless you edit the code).
491
492 The text from the shortkeys file is sent to the keyboard using the send_input API function. With using the C<use.ctrl_v='1'> attribute in a data element, the text will be place in the clipboard and paste (with sending the keys ctlr + v) at the cursor position.
493
494 <data k= 'a' use.ctrl_v= '1'>
495 This text will be copied and paste.
496 And the new line will be preserved.
497 </data>
498
499
500 In the xml file, data elements can be combine using a dataref element.
501
502 <data k='qu'>10.1080/14737159.2017.1289087</data>
503 <data k= 'u'>
504 Published version; http://dx.doi.org/<dataref id= 'qu'></dataref>
505 </data>
506
507 When hitting <u, the text that will be subtitued will be Published version; http://dx.doi.org/10.1080/14737159.2017.1289087
508
509 =head2 Commands syntax in shortkey.xml
510
511 =over
512
513 =item * a command keystroke start with # (to diplay # as a character, it has to be enter has ##), next you have to give
514
515 =item * the command itself, set by a character (only one character) listed in the map defined with the property vkcode_map
516
517 vkcode_map:
518 t: VK_TAB
519 e: VK_RETURN
520 ...
521
522 The character z is hardcoded to indicate a waiting time : in the shortkeys_utf8.xml file C<#z04>
523 will calls the code
524
525 usleep ( 4 * 100_000 );
526
527 If z is used to indicate a key in vkcode_map, this will be overriden.
528
529 =item * how much you want to repeat that command, on two position, with a padding 0 if necessary (01)
530
531 =item * the next characters are treated as text (unless a new command keystroke is defined with #)
532
533 =item * The shift, control and alt keys are released
534
535 =over
536
537 =item * after a non-command key has been given. For example ctr+shift+a (written as #c01#s01a) will send the following event: key press for the keys control and shift, key press and released for the a key, key release for shift and control
538
539 =item * at the end of a command keystroke, if the keys have not been released. For example a sequence of shit+tab, shift+tab, shift+tab (#s01#t01#t01#t01) will release the shift key at the end. On the contrary #s01#t01#t01#t01abc will call three back tab and will write Abc.
540
541 =back
542
543 =back
544
545 =head1 INSTALLATION
546
547 To install this module type the following:
548
549 perl Makefile.PL
550 make
551 make test
552 make install
553
554 On windows use nmake or dmake instead of make.
555
556 =head1 DEPENDENCIES
557
558 The following modules are required in order to use this module
559
560 Test::Simple => 0.44,
561 Win32::Shortkeys::Kbh => 0.01,
562 Config::YAML::Tiny => 1.42,
563 Win32::Clipboard => 0.58,
564 XML::Parser => 2.44,
565 Encode => 2.84,
566 Time::HiRes => 1.9733,
567 Carp => 1.40
568
569 =head1 SEE ALSO
570
571 L<Win32::Shortkeys::Ripper>
572
573 L<Win32::Shortkeys::Kbh>
574
575 =head1 SUPPORT
576
577 Any questions or problems can be posted to me (rappazf) on my gmail account.
578
579 The current state of the source can be extract using Mercurial from
580
581 L<http://sourceforge.net/projects/win32-shortkeys/>.
582
583 =head1 AUTHOR
584
585 FranE<ccedil>ois Rappaz <rappazf@gmail.com>
586
587
588 =head1 COPYRIGHT
589
590 This program is free software; you can redistribute
591 it and/or modify it under the same terms as Perl itself.
592
593 The full text of the license can be found in the
594 LICENSE file included with this module.
595
596
597 =cut
598
599 1;
600
601
602
« Shortkeys/Manager.pm ↑ Summary