1. Hot/Accelerator Keys

Okay, I give up, how does one implement Hot/Accelerator keys?  I searched
the mailing list to no avail.  Apparently it must be an easy excercise  . .
.?  I know about using the "&" symbol to precede a letter in the control
definitions, but how do I access this in code?  Please tell me I don't have
to use the onKeyDown event!

new topic     » topic index » view message » categorize

2. Hot/Accelerator Keys

-- demo of hiding a window, and getting it back,
-- or changing colors, by pressing a hotkey combination
-- originally by Eddy Van Esch, 15/01/2001

include win32lib.ew
without warning

constant
WM_HOTKEY = #312,
MOD_CTRL = #2,
key = 'S',   -- hotkeys are CTRL S,Z,X... change as necessary
key2 = 'Z',
key3 = 'X',

zRegisterHotKey = registerw32Function( user32,
         "RegisterHotKey",{ C_LONG, C_LONG, C_LONG, C_LONG }, C_LONG ),
zUnregisterHotKey = registerw32Function( user32,
         "UnregisterHotKey",{ C_LONG, C_LONG }, C_LONG )

atom ok
sequence msg
msg="Click  'Hide window'  button to hide this window.\n" &
    "Press hotkey combination CTRL S to show the window again.\n\n" &
    "CTRL Z, and CTRL X change the window's colors."

constant
Win1 = create( Window, "HotKey Demo", 0, Default, Default,400, 200, 0 ),
HideButton = create( PushButton, "Hide window", Win1,120, 12, 144, 40, 0 ),
TheTxt = create( LText, msg, Win1, 40, 70, 300, 70, 0 )

-- Hide button's behavior
procedure onClick_HideButton()
  setVisible( Win1, False )  -- Hide the window
end procedure

-- Event handler: intercept the hotkey and define what to do with it:
procedure Events(atom iMsg, atom wparam, atom lparam)

  if iMsg = WM_HOTKEY then
    if hi_word(lparam) = 'S' then
      --unhide window, can be used to do anything you
      --want here...
      setVisible( Win1, True )
    elsif hi_word(lparam) = 'Z' then
      setWindowBackColor(Win1,Yellow)
      setWindowBackColor(TheTxt,Yellow)
    elsif hi_word(lparam) = 'X' then
      setWindowBackColor(Win1,getSysColor(COLOR_BTNFACE))
      setWindowBackColor(TheTxt,getSysColor(COLOR_BTNFACE))
    end if
  end if
end procedure

procedure register_keys()
-- register hotkeys
  ok = w32Func(zRegisterHotKey, {getHandle(Win1), 1, MOD_CTRL, key})
  ok = w32Func(zRegisterHotKey, {getHandle(Win1), 2, MOD_CTRL, key2})
  ok = w32Func(zRegisterHotKey, {getHandle(Win1), 3, MOD_CTRL, key3})
end procedure

--
onClick[HideButton] = routine_id( "onClick_HideButton" )
onEvent[Win1] = routine_id( "Events" )
onOpen[Win1] = routine_id("register_keys")

WinMain( Win1, Normal )  --normal event loop

new topic     » goto parent     » topic index » view message » categorize

3. Re: Hot/Accelerator Keys

Okay, maybe there is something wrong somewhere . . .  I have a pushbutton
called "Remove", the accelerator being "Alt-R", yet the button is not pushed
when I press "Alt-R", the focus just changes to the button . . .  This
happens to all my pushbuttons in the window . . .  The focus changes to the
desired button, but the button is not pressed . . .

----- Original Message -----
From: "Derek Parnell" <ddparnell at bigpond.com>
To: "EUforum" <EUforum at topica.com>
Subject: RE: Hot/Accelerator Keys


>
> Hi "jjnick at cvn.com",
>
> > Okay, I give up, how does one implement Hot/Accelerator keys?
> >  I searched
> > the mailing list to no avail.  Apparently it must be an easy
> > excercise  . .
> > .?  I know about using the "&" symbol to precede a letter in
> > the control
> > definitions, but how do I access this in code?  Please tell
> > me I don't have
> > to use the onKeyDown event!
>
> what are you trying to do with hot keys?
>
> When you say "how do I access this in code?", what is the "this" you are
> referring to? Are you asking how to know which are the hot keys associated
> with a given control? Or are you wanting to know which hot key is pressed?
> Or are you trying the get your code to react to a hotkey press?
>
> To associate a hot key with a specific control use ...
>
>     registerHotKey(id, keycode)
>
> eg.
>     -- Make the Alt-F9 key "press" the button.
>     registerHotKey( btnClose, VK_F9)
>
> To get a list of hot keys and the associated controls, use ...
>
>     sequence rdata
>     -- Get the list of controls for this parent window that have hots
keys,
>     -- and a list of the hotkey keycodes.
>     rdata = getControlInfo( myWin, {CONTROLINFO_hotkey_ids,
>                                     CONTROLINFO_hotkey_keys})
>
>     for i = 1 to length(rdata[1]) do
>           -- Control rdata[1][i] has hotkey rdata[2][i]
>     end for
>
> A hotkey sets the focus to the control that owns the hotkey, thus causing
a
> GotFocus event. If that control is a button, it also causes the Click
event
> to happen. Thus normally one codes a handler for GotFocus or Click to trap
> hotkey presses. But it doesn't distinguish between a hotkey and a normal
> invocation.
>
> Do you need to know if either of these two events were caused by a hotkey
> rather than the normal triggering event, such as a TAB key or MouseClick
or
> SpaceBar for buttons? If so, you'd have to use the KeyDown event to catch
> that.
>
> -----------
> cheers,
> Derek Parnell
>

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu