如何检查Gtk3中的按键和特定的修饰符?例如,在这里我要检查是否按下了 CTRL + s :
use feature qw(say);
use strict;
use warnings;
use Gtk3 '-init';
use Glib qw(TRUE FALSE);
my $window = Gtk3::Window->new('toplevel');
$window->set_title('Hello world');
$window->set_position('center');
$window->set_default_size(500, 500);
$window->signal_connect('delete-event' => sub {
Gtk3->main_quit();
});
$window->signal_connect('key-press-event' => sub { handle_key( @_ ) } );
$window->show_all();
Gtk3->main();
sub handle_key {
my ( $widget, $event) = @_;
my $key = Gtk3::Gdk::keyval_name( $event->keyval );
if ( ($key eq 's') && control_pressed( $event ) ) {
say "CTRL+S";
}
return FALSE; # FALSE -> means propagate key further
}
我应该如何定义上面的功能control_pressed()
?
答案 0 :(得分:0)
$event->state
的类型为Gtk3::Gdk::ModifierType
,具有overloaded operators。
旗帜以过载的形式具有一些额外的魔法能力 运算符:
+ or | union of two flagsets ("add") - difference of two flagsets ("sub", "remove") * or & intersection of two bitsets ("and") / or ^ symmetric difference ("xor", you will rarely need this) >= contains-operator ("is the left set a superset of the right set?") == equality
此外,布尔上下文中的标志 指出它们是否为空,这使您可以编写 自然地进行常见操作:
$widget->set_events ($widget->get_events - "motion_notify_mask"); $widget->set_events ($widget->get_events - ["motion_notify_mask", "button_press_mask"]); # shift pressed (both work, it's a matter of taste) if ($event->state >= "shift-mask") { ... if ($event->state * "shift-mask") { ... # either shift OR control pressed? if ($event->state * ["shift-mask", "control-mask"]) { ... # both shift AND control pressed? if ($event->state >= ["shift-mask", "control-mask"]) { ...
此外,GdkModifierType enum的常量在Perl中变为小写字符串。因此,以下方法应该起作用:
sub control_pressed {
my ( $event ) = @_;
return $event->state & 'control-mask';
}