本代码:
实现了数字0..10,大小写字母a..z字符的录入,将"-"变为10,其他键输出Virtual keycode of the key 。
功能键:Esc、Backspace、enter、4个方向键、大小写切换、小键盘切换可用。
按enter键时,附加保存最后一行至txt文档。
- #!/usr/bin/perl
- use threads;
- use Thread::Queue;
- use Win32::Console;
- use feature ":5.10";
- my $In = new Win32::Console(STD_INPUT_HANDLE) || die;
- my $Out = new Win32::Console(STD_OUTPUT_HANDLE) || die;
- #多线程生产者消费者
- my $q = Thread::Queue->new();
- my $producer = threads->create( \&produce );
- my $consumer = threads->create( \&consume );
- $producer->join();
- $consumer->join();
- sub produce {
- while (1) {
- my @event = $In->Input();
- # keyboard event type = 1 key down=1 repeat count =1
- if ( ($event[0] == 1)&&($event[1] == 1)&&($event[2] == 1) ) {
- my ( $virtualKey, $asciiKey ) = @event[ 3, 5 ];
- given ($virtualKey) {
- when (27) { exit 0 }
- when (/20|144/) { next }
- when (32) { $Out->Write(" ") }
- when ([48,96]) { $Out->Write("0 ") }
- when (189) { $Out->Write("10 ") }
- when (8) {
- my ( $x, $y ) = $Out->Cursor();
- $Out->Cursor( $x - 2, $y );
- $Out->Write(" ")
- }
- when (13) { $Out->Write("\n"); save_chr() }
- when (37) {
- my ( $x, $y ) = $Out->Cursor();
- $Out->Cursor( $x - 1, $y )
- }
- when (39) {
- my ( $x, $y ) = $Out->Cursor();
- $Out->Cursor( $x + 1, $y )
- }
- when (38) {
- my ( $x, $y ) = $Out->Cursor();
- $Out->Cursor( $x, $y - 1 )
- }
- when (40) {
- my ( $x, $y ) = $Out->Cursor();
- $Out->Cursor( $x, $y + 1 )
- }
- when ([ 49 .. 57, 65 .. 90, 97..105 ]) { $q->enqueue( chr($asciiKey) ) }
- default { $q->enqueue($virtualKey) }
- }
- }
- }
- sleep(1);
- }
- sub consume {
- while ( my $key = $q->dequeue() ) {
- $Out->Write("$key ");
- }
- }
- sub save_chr {
- my @info = $Out->Info();
- my $Rchar = $Out->ReadChar( $info[0], 0, $info[3] - 1 );
- open( FH, ">>Result.txt" );
- print FH $Rchar . "\n";
- close(FH);
- }
很早以前放在QQ空间上的文件,转过来这里。
阅读(1384) | 评论(0) | 转发(0) |