Thread Tk MainWindow per Shortcut Minimieren (8 answers)
Opened by Juergen at 2011-02-09 08:55

Kalle
 2011-02-12 13:19
#145603 #145603
User since
2007-03-18
48 Artikel
BenutzerIn
[default_avatar]
Hallo Jürgen,

Bei mail-archive.com hab ich einen Beitrag von Steve Loughran vom 09.11.2006 gefunden, in dem er aus Perl heraus mit Hilfe von Win32:API einen systemweiten Hotkey erstellt.
(siehe: http://www.mail-archive.com/perl-win32-gui-users@lists.sourceforge.net/msg04906.html)

Habe nichts getestet, möchte nur zitieren. Vielleicht kannst du daraus eine Lösung für dich ableiten:
Quote
Sorry to dredge this topic up, but I thought I would post my demo code to create system-wide hotkeys. Feel free to poke holes in my coding style (I wont be listening :) )

Its a modified version of the code that Robert May posted for the generic "bring the window to the front" hotkey code. This version calls a specific routine when the hotkey is pressed. On the 6th press, the code will clean up and exit

I hope this is helpful to someone.

Steve


Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!perl -w

use strict;
use warnings;

use Win32::GUI 1.03_04, qw( WM_HOTKEY VK_A );
use Win32::API;

sub HOTKEYF_SHIFT()   {0x01}
sub HOTKEYF_CONTROL() {0x02}
sub HOTKEYF_ALT()     {0x04}
sub HOTKEYF_EXT()     {0x08}

my $hotkey_count = 0;
# Registering a hotkey requires a Unizue "ID" from the system
# Use GlobalAddAtom to request this "ID"
my $GlobalAddAtom = Win32::API->new("kernel32","GlobalAddAtom", "P", "L")
        or die "Failed to load GlobalAddAtom: $!";

# After use, the Atom should be returned to the system pool
my $GlobalDeleteAtom = Win32::API->new("kernel32","GlobalDeleteAtom", "L", "L")
        or die "Failed to load GlobalDeleteAtom: $!";

my $RegisterHotKey = Win32::API->new("user32","RegisterHotKey", "LIII", "I")
        or die "Failed to load RegisterHotKey: $!";

my $UnregisterHotKey = Win32::API->new("user32","UnregisterHotKey", "LI", "I")
        or die "Failed to load UnregisterHotKey: $!";

my $mw = Win32::GUI::Window->new(
        -size => [400,300],
        -title => "Hotkey $hotkey_count / 5"
);
# request an ID or Atom using a unique string
my $atom = $GlobalAddAtom->Call("hotkey-string")
        or die "unable add atom: $!\n";

my $hotkey = VK_A;
my $modifier = HOTKEYF_CONTROL | HOTKEYF_ALT | HOTKEYF_SHIFT;

unless ($RegisterHotKey->Call($mw->{-handle}, $atom, $modifier, $hotkey))
        {
        print "RegisterHotKey for $atom failed\n";
        #if the system fails, you must return Atom to system
        # because ther loiter around long after your code has died
        $GlobalDeleteAtom->Call($atom);
        exit 0;
        }
        
#now hook the WM_HOTKEY message call    
unless ($mw->Hook(WM_HOTKEY, \&process_hotkeys))
        {
        print "hook failed\n";
        $UnregisterHotKey->Call($mw->{-handle}, $atom);
        $GlobalDeleteAtom->Call($atom);
        exit 0; 
        }

$mw->Show();
while (Win32::GUI::DoEvents() != -1
                and $hotkey_count <= 5)
        {
        select(undef, undef, undef, 0.01);
        }

print "6th press of hotkey, clearing up and exiting\n";
#unregister the hotkey
$UnregisterHotKey->Call($mw->{-handle}, $atom);
#return the atom
$GlobalDeleteAtom->Call($atom);

exit 0; 

sub process_hotkeys
        {
        my ($object, $wParam, $lParam) = @_;
        if ($wParam == $atom)
                {
                $hotkey_count++;
                print "found my atom -> $atom\n";
                print "Count now $hotkey_count\n";
                $mw->Change( -title     => "Hotkey $hotkey_count / 5");
                }
        return;
        }
        


Last edited: 2011-02-13 02:50:08 +0100 (CET)

View full thread Tk MainWindow per Shortcut Minimieren