Sign in to follow this  
Followers 0

GUI Req: Local hotkeys

43 posts in this topic

Posted (edited)

Short:

Window-specific hotkeys (for GUI).

Long:

I would like to see "Local hotkeys".

Hotkeys that are attached to a GUI window.

When you set the hotkey and switch to another application/script/window, the hotkey is not trapped by AutoIt.

Examples:

Ctrl+S from Notepad doesn't trap hotkey from IE.

Enter from Calculator doesn't trap key from Notepad.

Suggestion:

- Rename Add alias for HotKeySet as: HotKeySetGlobal

- Name the local hotkeyset function: HotKeySetLocal

Edited by SlimShady

Share this post


Link to post
Share on other sites



Posted

Some time ago someone asking for this. And I agree, this will be an excellent addition. But I'm not remember, where HotKeySet first appear. If in the previous release, this renaming may break current scripts or we should add alias... Maybe better just add HotKeySetLocal. If this addition will be approved at all :idiot:

Share this post


Link to post
Share on other sites

Posted (edited)

I forgot to mention that.

Previous versions should not break. So it's better to use an alias instead of a rename.

Edited by SlimShady

Share this post


Link to post
Share on other sites

Posted (edited)

Since this would be explicitely for GUI usage, perhaps use the GUI naming conventions?

GUISetHotKey? Have it work just like the GUISet... functions, so it works individually with each GUI.

ie:

GUICreate('Main')
GUISetBkColor(0x00ff00)
GUISetHotKey('a', 'MainHotKey')
GUICtrlCreateLabel('Press Backspace', 5, 5)

GUICreate('Second')
GUISetHotKey('a', 'SecondHotKey')
GUICtrlCreateLabel('Press "A"', 5, 5)

Func MainHotKey()
     MsgBox(0, '', 'A was pressed in the main GUI')
EndFunc

Func SecondHotKey()
     MsgBox(0, '', 'A was pressed in the secondary GUI')
EndFunc

Or hey, maybe even if they were setup like controls... think about it.

GuiCreate('Press the "A" key')
$hotkey = GuiCtrlCreateHotKey('a')

While 1
     $msg = GuiGetMsg()

     If $msg = $hotkey Then
          msgbox(0,'','The "A" key was pressed')
     EndIf
WEnd

In fact, I think I like that mode best of all.

Edited by Saunders

Share this post


Link to post
Share on other sites

Posted

Sure.

Good idea by the way.

Share this post


Link to post
Share on other sites

Posted

Since this would be explicitely for GUI usage, perhaps use the GUI naming conventions?

Is this true. If I understand correctly the original poster he is talking about attaching a HotKey to a particular window, but not necesarily a window that was created by AutoIt. Instead we wants to attach the hotkey to an external program window, like Notepad, etc.

Isn't that right?

Angel

Share this post


Link to post
Share on other sites

Posted (edited)

No, he wants to create hotkeys for a window created by AutoIt.

For example a GUI-script where you can control a car by using hotkeys, and the

hotkeys only work when the window is active. Like every other games...

@ SlimShady :

SlimShady, I'm pretty sure you know this, but that you want a new function for

AutoIt that can do it the fancy way.. Anway, you could create a script does the

same.. Something like this maybe :

Select
Case WinActive($wTitle)
; Assign hotkeys

Case Else
; Unassign hotkeys
EndSelect

Anyway, I liked your idea..

Edited by Helge

Share this post


Link to post
Share on other sites

Posted (edited)

Is this true. If I understand correctly the original poster he is talking about attaching a HotKey to a particular window, but not necesarily a window that was created by AutoIt. Instead we wants to attach the hotkey to an external program window, like Notepad, etc.

Isn't that right?

Angel

<{POST_SNAPBACK}>

No, no, no. Saunders understood me correctly.

I want hotkeys to be attached to an AutoIt-made GUI/window.

That Notepad thing was just an example I made up.

Edited by SlimShady

Share this post


Link to post
Share on other sites

Posted

@ SlimShady :

SlimShady, I'm pretty sure you know this, but that you want a new function for

AutoIt that can do it the fancy way.. Anway, you could create a script does the

same.. Something like this maybe :

Select
Case WinActive($wTitle)
; Assign hotkeys

Case Else
; Unassign hotkeys
EndSelect

Anyway, I liked your idea..

<{POST_SNAPBACK}>

1. Do you know how many times the script will (un)assign the hotkey per second with a 25ms sleep?

40 times. Make it 35. That's unnecessary for AutoIt AND the CPU.

2. I would have to that for every script.

If I assign the hotkey just once at the top of the script, that would save me time and save CPU-time.

Share this post


Link to post
Share on other sites

Posted

I thought you might say that, but you could create a simple check that solves that..

I'm using local hotkeys in one of my scripts, and it works very well.

What you do, is just adding a simple check :

Select
Case WinActive($wTitle)
If $keysAssigned = 0 Then
; Assign hotkeys
$keysAssigned = 1
EndIf

Case Else
If $keysAssigned = 1 Then
; Unassign hotkeys
$keysAssigned = 0
EndIf
EndSelect

Simple, but works..

Share this post


Link to post
Share on other sites

Posted

It gets kind of annoying when you have multiple GUIs in one application though. You end up with code like (to borrow from your example):

Select
    Case WinActive('Window1')
        If $keysAssigned <> 'Win1' Then
        ; Assign hotkeys for window 1
        ; Unassign hotkeys for all other windows
            $keysAssigned = 'Win1'
        EndIf
    Case WinActive('Window2')
        If $keysAssigned <> 'Win2' Then
        ; Assign hotkeys for window 2
        ; Unassign hotkeys for all other windows
            $keysAssigned = 'Win2'
        EndIf
    Case Else
        If $keysAssigned <> 'None' Then
        ; Unassign all hotkeys
            $keysAssigned = 'None'
        EndIf
EndSelect

And that's just two GUIs. I understand where you're coming from, this is a solution for now, but I see it as a workaround to something that could be implemented nicely into the GUI functions.

Personally, I currently use functions like this:

Func ToggleHotKeys($iTog, $sKeyList = 'Main')
    $aKeys_Main = StringSplit('{ESC}.^s.^{DEL}.^+{DEL}.!t.!h.^m.^2', '.');
    $aFuncs_Main = StringSplit('EscapeKey.SwitchKey.ClearOneKey.ClearAllKey.SettingsTab.SwitchUsersTab.DoMiniMode.DoMiniMode', '.');

    $aKeys_Mini = StringSplit('^f.^1', '.');
    $aFuncs_Mini = StringSplit('DoFullMode.DoFullMode', '.');

    $aKeys = Eval('aKeys_' & $sKeyList);
    $aFuncs = Eval('aFuncs_' & $sKeyList);
    For $i = 1 to $aKeys[0]
        If $iTog Then
            HotKeySet($aKeys[$i], $aFuncs[$i]);
        Else
            HotKeySet($aKeys[$i]);
        EndIf
    Next
EndFunc;ToggleHotKeys

And then I have this function adlibbed:

Func ActiveCheck()
    If WinActive($gui_Mini) Then
        ToggleHotKeys(1, 'Mini');
    ElseIf WinActive($gui_Main) Then
        ToggleHotKeys(1, 'Main');
    Else
        ToggleHotKeys(0, 'Main');
        ToggleHotKeys(0, 'Mini');
    EndIf
EndFunc

But I think it'd be nice if we could do away with all of this and just have something like:

$gui_Editor = GuiCreate('Editor')
GuiCtrlCreateHotKey('^s', 'Save')
GuiCtrlCreateHotKey('^o', 'Open')

Don't you think that would be nice?

Share this post


Link to post
Share on other sites

Posted

Man... I would love to see this.

Share this post


Link to post
Share on other sites

Posted

Man... I would love to see this.

It couldn't be done with the hotkey API as that just allows you to create a system wide hotkey, you can't pin it down to a single app.

But at least for CTRL+, ALT+ type keys there are some things called "accelerators". You usually see them in window menus, like next to Open you usually see CTRL+O. That could probably be added in fairly easily (without having to have a menu).

Single keys would be too complicated though I think.

Share this post


Link to post
Share on other sites

Posted

It couldn't be done with the hotkey API as that just allows you to create a system wide hotkey, you can't pin it down to a single app.

But at least for CTRL+, ALT+ type keys there are some things called "accelerators".  You usually see them in window menus, like next to Open you usually see CTRL+O.  That could probably be added in fairly easily (without having to have a menu). 

Single keys would be too complicated though I think.

<{POST_SNAPBACK}>

Adding accelerators is as easy as pre-fixing the key you wish to be the accelerator with &. Then Alt+key will work. For example, I just tested in a GUI with this line:

$g_btnDownload = GUICtrlCreateButton("&Download", 95, 375, 71, 20)

Pressing Alt+D in the GUI triggers that button.

Share this post


Link to post
Share on other sites

Posted

Accelerators is which we need.

Sometimes I use Resource Hacker to customize system files.

One day when I was editing Notepad2. I decided to check out the accelerators section.

And I changed the hotkey/accelerator that shows the properties window of the current file, to Alt+Enter.

I changed it to this:

VK_RETURN, 40015, NOINVERT, ALT, VIRTKEY

I'll search for some links about the "Accelerators" subject.

http://msdn.microsoft.com

http://www12.canvas.ne.jp/peters/colin/win...rces/accel.html

http://www.xploiter.com/programming/c/borland/3163.html

http://www.tenermerx.com/owlhow/items/tutorial/step2.html

Share this post


Link to post
Share on other sites

Posted

Yes, accelerator keys! Although I didn't realize it would be hard to use single keys. Ever used IrfanView? Many of it's accelerators are single keys. But looking at it's accelerators section in ResHacker gives back some unreadable results...

IRFANVIEW ACCELERATORS
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
{
    9421, 20188, SHIFT, ASCII
    23827, 1668, CONTROL, SHIFT, ASCII
    29314, 24666, SHIFT, VIRTKEY
    3957, 18712, NOINVERT, SHIFT, VIRTKEY
    2739, 39015, SHIFT, VIRTKEY
    [...snip...]
}

I've reshacked my notepad in Win98 to add all kinds of accelerators to it. It didn't come with Ctrl+S, Ctrl+O for saving etc, so I added it manually. On a side note I've also used ResHacker to move menu entries around in the Notepad on WinXP, but that doesn't really have anything to do with this conversation. :lmao:

Share this post


Link to post
Share on other sites

Posted

So what's the story here. Is it going ahead? Next version? I was just playing with Resource Hacker and was able to add single key hotkeys to Notepad just like any other. Sure I can't type an 'A' into the window anymore, but it works haha.

Share this post


Link to post
Share on other sites

Posted

I'd like to see this too, I'd did a work around that requires you to add 1 line to the start of you func thats called via the hotkey. So I guess if you need it now you could try:

#include <GUIConstants.au3>

$hGUI1  = GUICreate("GUI 1",100,100,-1,0)
GUISetState(@SW_SHOW,$hGUI1)

HotKeySet("a","Func1")

While GUIGetMSG() <> $GUI_EVENT_CLOSE
Wend


Func Func1()
    if Not GUIHotKeyCheck($hGUI1,"a","Func1") Then Return
    MsgBox(0,"","Hotkey pressed")
EndFunc

Func GUIHotKeyCheck($hGUI, $szHotKey, $szFunc)
    if Not WinActive($hGUI) Then
        HotKeySet($szHotKey)
        Send($szHotKey)
        HotKeySet($szHotKey,$szFunc)
        Return 0
    Endif
    Return 1
EndFunc

Share this post


Link to post
Share on other sites

Posted

that's similar to what i do. i still want these functions made though

Share this post


Link to post
Share on other sites

Posted (edited)

I was thinking, being able to pass arguments to a HotKey would make this easier, because if I could pass arguments to a function called via a hotkey I could just make my own GUIHotKeySet that would not require me to changed the called function.

EDIT

Even if the argument passed was a constant / ByRef wasnt allowed

Edited by Ejoc

Share this post


Link to post
Share on other sites
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.