0

我怎样才能捕捉到shift-some-key与这个脚本的组合?
当我按下箭头键时,我得到了我期望的结果,但是当我按下shift-tab它不会返回KEY_BTAB值。

use warnings;
use 5.12.0;
use Win32::Console qw(STD_INPUT_HANDLE ENABLE_MOUSE_INPUT);
use constant {
    RIGHT_ALT_PRESSED  => 0x0001,
    LEFT_ALT_PRESSED   => 0x0002,
    RIGHT_CTRL_PRESSED => 0x0004,
    LEFT_CTRL_PRESSED  => 0x0008,
    SHIFT_PRESSED      => 0x0010,

    VK_LEFT     => 0x25,
    VK_UP       => 0x26,    
    VK_RIGHT    => 0x27,    
    VK_DOWN     => 0x28,    
    VK_TAB      => 0x09,    
};
use constant SHIFTED_MASK =>
    RIGHT_ALT_PRESSED |
    LEFT_ALT_PRESSED |
    RIGHT_CTRL_PRESSED |
    LEFT_CTRL_PRESSED |
    SHIFT_PRESSED;

my %d = (
    KEY_DOWN            => 258,
    KEY_UP              => 259,
    KEY_LEFT            => 260,
    KEY_RIGHT           => 261,
    KEY_BTAB            => 353,
);

my $con_in = Win32::Console->new(STD_INPUT_HANDLE);
$con_in->Mode(ENABLE_MOUSE_INPUT);

while ( 1 ) {
        my $key = getch();
        say "<$key>";
        last if $key == 113;
}

sub getch {
    my @event = $con_in->Input();
    my $event_type = shift( @event );
    if ( defined $event_type and $event_type == 1 ) { 
        my ( $key_down, $repeat_c, $vkcode, $vsccode, $char, $ctrl_ks ) = @event;
        if ( $char ) {
            return $char;
        }
        else {
            if ( $vkcode == VK_UP and ( $ctrl_ks & SHIFTED_MASK ) == 0 ) {
                return $d{KEY_UP};
            }
            elsif ( $vkcode == VK_DOWN and ( $ctrl_ks & SHIFTED_MASK ) == 0 ) {
                return $d{KEY_DOWN};
            }  
            elsif ( $vkcode == VK_RIGHT and ( $ctrl_ks & SHIFTED_MASK ) == 0 ) {
                return $d{KEY_RIGHT};
            }        
            elsif ( $vkcode == VK_LEFT and ( $ctrl_ks & SHIFTED_MASK ) == 0 ) {
                return $d{KEY_LEFT};
            } 
            elsif ( $vkcode == VK_TAB and $ctrl_ks == SHIFT_PRESSED ) {
                return $d{KEY_BTAB};    # <--
            }
            else {
                say "beep";
            }
        }
    }
}

当我按下shift和时输出tab

<1>
<9>
<9>

<1>

以这种方式编辑getch例程后

sub getch {
    my @event = $con_in->Input();
    my $event_type = shift( @event );
    if ( defined $event_type and $event_type == 1 ) { 
        my ( $key_down, $repeat_count, $virtual_keycode, $virtual_scancode, $char, $ctrl_key_state ) = @event;
        if ( $char ) {
            if ( $key_down ) {
                return $char for $repeat_count;
            }
        }
        else {
            if ( $virtual_keycode == VK_UP and ( $ctrl_key_state & SHIFTED_MASK ) == 0 ) {
                if ( $key_down ) {
                    return $d{KEY_UP} for $repeat_count;
                } 
            }
            elsif ( $virtual_keycode == VK_DOWN and ( $ctrl_key_state & SHIFTED_MASK ) == 0 ) {
                if ( $key_down ) {          
                    return $d{KEY_DOWN} for $repeat_count;
                }
            }  
            elsif ( $virtual_keycode == VK_RIGHT and ( $ctrl_key_state & SHIFTED_MASK ) == 0 ) {
                if ( $key_down ) {
                    return $d{KEY_RIGHT} for $repeat_count;
                }
            }        
            elsif ( $virtual_keycode == VK_LEFT and ( $ctrl_key_state & SHIFTED_MASK ) == 0 ) {
                if ( $key_down ) {
                    return $d{KEY_LEFT} for $repeat_count;
                }
            } 
            elsif ( $virtual_keycode == VK_TAB and ( $ctrl_key_state & SHIFTED_MASK ) == SHIFT_PRESSED ) {
                if ( $key_down ) {
                    return $d{KEY_BTAB} for $repeat_count;
                }
            }
            else {
                say "beep";
            }
        }
    }
}

我得到这个输出:
beep
<1>
<9>
<0>
beep
<1>

4

2 回答 2

3

当我运行您的代码时,当我点击+时,$ctrl_ks其值为 48 (0x0030) ,释放时为 32 (0x0020) 。我认为您要进行的检查是ShiftTabShift

elsif ($vkcode==VK_TAB and ($ctrl_ks & SHIFT_PRESSED)==SHIFT_PRESSED) {
    return $d{KEY_BTAB};
于 2012-04-17T17:27:35.070 回答
1

首先,$char设置为 9,所以你永远不会得到你的支票。将if ($char)支票移到更合适的地方。


其次,你的检查是错误的。如果 Caps Lock 处于打开状态,则以下操作将不起作用。

elsif ( $vkcode == VK_TAB and $ctrl_ks == SHIFT_PRESSED )

您应该只检查您感兴趣的标志。

elsif ($vkcode==VK_TAB and ( $ctrl_ks & SHIFTED_MASK ) == SHIFT_PRESSED)

最后,有时您只会收到一次多次按下的通知。这是由 发出的信号$repeat_count。您忽略了这一点,因此您可能会忽略键。

您尝试$repeat_count在第二个片段中处理,但惨遭失败。部分问题是您for $repeat_count应该从我的其他答案中复制for 1..$repeat_count,另一个问题是即使$repeat_count大于一个值,您也只返回一个值。


my @kbd_queue;
sub getch {
    my @event;
    if (@kbd_queue) {
       @event = ( 1, @{ pop @kbd_queue } );
    } else {
       @event = $con_in->Input();
    }

    my $event_type = shift( @event );
    if ( defined $event_type and $event_type == 1 ) { 
        my ( $key_down, $repeat_count, $virtual_keycode, $virtual_scancode, $char, $ctrl_key_state ) = @event;
        return -1 if !$key_down;

        if ( $virtual_keycode == VK_UP and ( $ctrl_key_state & SHIFTED_MASK ) == 0 ) {
            push @kbd_queue, \@event for 2..$repeat_count;
            return $d{KEY_UP};
        } 
        ...
        elsif ( $virtual_keycode == VK_TAB and ( $ctrl_key_state & SHIFTED_MASK ) == SHIFT_PRESSED ) {
            push @kbd_queue, \@event for 2..$repeat_count;
            return $d{KEY_BTAB};
        }
        elsif ( $char ) {
            push @kbd_queue, \@event for 2..$repeat_count;
            return $char;
        }
        else {
            say "beep";
        }
    }
}

你应该把它转换成表格驱动的东西。

于 2012-04-17T19:36:05.267 回答