On 11/07/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
I have a small program which can be minimized to the tray.
Now I want to have an accelerator which fires to minimize the program to the
tray => this works fine.
But now I want to restore the window with an accelerator anytime.
It should work if I'm writing a text or in internet explorer or firefox ... 
everytime.
is this possible?
how can I install the accelerator so that it can be activated?

Yes, it's possible, you'll find it discussed on this list if you
search for WM_HOTKEY or WM_SETHOTKEY (these are 2 slightly different
techniques.

I'll attach some code that shows the WM_SETHOTKEY method to this mail
- it doesn't quite meet your requirements, but it's close and can be
done with Win32::GUI alone.  I'll follow-up with code that uses the
Win32 RegisterHotKey() function - I think it exactly meets what you
want to do, but needs Win32::API() to call the functions that are
missing from Win32::GUI.

Regards,
Rob.

#!perl -w
use strict;
use warnings;

# What this script does:
# Creates a Window, and a notify icon.
# Register a Hotkey (Ctrl-A) for the window.
# When the window is minimised, it is not hidden.
# Left-clicking on the notify icon, or pressing the
# Hotkey combination will Show the window again.

# In this example, we can't hide the window, as if we do, then
# I can't find a suitable hook into the message system to know
# when to show the window again.

use Win32::GUI 1.05 qw( CW_USEDEFAULT
                       WM_SETHOTKEY
                       VK_A
                       IDI_DEFAULTICON );

# Some constants missing from Win32::GUI::Constants
sub HOTKEYF_SHIFT()   {0x01} # Shift key
sub HOTKEYF_CONTROL() {0x02} # Ctrl key
sub HOTKEYF_ALT()     {0x04} # Alt key
sub HOTKEYF_EXT()     {0x08} # Extended key (??)

my $mw = Win32::GUI::Window->new(
   -title => 'HotKey demonstration',
   -left  => CW_USEDEFAULT,
        -size  => [400,300],
);

my $icon = Win32::GUI::Icon->new(IDI_DEFAULTICON);

$mw->AddNotifyIcon(
   -icon => $icon,
   -onClick => \&show_window,
);

set_hotkey($mw, VK_A, HOTKEYF_CONTROL);

$mw->Show();
Win32::GUI::Dialog();

sub show_window {
   my ($win) = @_;
   $win->Show();
   return 0;
}

# Set the hotkey for a window (or replace it if this is called
# more than once)
sub set_hotkey {
   my ($win, $key, $modifier) = @_;
   # $win - any top level window
   # $key - any virtual keycode (VK_*) Esc, TAB, SPACE and a few others.
   # $modifier: any combination of the HOTKEYF_* constants (or 0 for
no modifier)

   # Watch out.  Despite what the MS documentation says the modifier
   # goes in the upper byte of a short, not the upper WORD of a
   # DWORD.  I.e. the left shift is 8 bits, not 16 bits
   my $wParam = ($modifier << 8) + $key;

   # Set the hotkey for the window by sending a WM_SETHOTKEY
   # message.  If successful pressing the hokey combination
   # results in a WM_SYSCOMMAND message being sent to the registered window
   # with wParam equl to SC_HOTKEY.  DefWindowProc process this by bringing
   # the window to the foreground.
   my $ret = $win->SendMessage(WM_SETHOTKEY, $wParam, 0);

   if($ret == -1) {
       die('set_hotkey() failed to register the hotkey - invalid hotkey');
   }
   elsif($ret == 0) {
       die('set_hotkey() failed to register the hotkey - invalid window');
   }
   elsif($ret == 1) {
       # success
   }
   elsif($ret == 2) {
       warn('set_hotkey(): registered hotkey, but another window has the '.
            "same hotkey.\nThe window activated by the hotkey will be".
            "randomly selected\n");
   }
   else {
       die('set_hotkey(): unexpected return value from WM_SETHOTKEY');
   }

   return;
}

Reply via email to