找回密码
 加入
搜索
查看: 7410|回复: 15

[GUI管理] [已解决]如何让同一个热键在不同控件获得焦点后执行不同内容?

  [复制链接]
发表于 2014-12-12 03:05:32 | 显示全部楼层 |阅读模式
本帖最后由 sh1536 于 2014-12-13 19:01 编辑

大概的情形是这样的:
有Input1,Button1和Input2,Button2,如何在Input1获得焦点后,Enter键就作为Button1的热键,相同的,当Input2获得焦点后Enter键就是Button2的热键。
发表于 2014-12-12 05:28:54 | 显示全部楼层
我的做法是设置个开关,在快捷键响应函数选择,欢迎指正
$Dummy = GUICtrlCreateDummy()
GUICtrlSetOnEvent($Dummy, "Dummy_ENTER")

Local $HotKeys[2][2] = [["{Enter}", $Dummy],["{TAB}", $Dummy]]
GUISetAccelerators($HotKeys, $Form1)



Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
        
        #forceref $hWnd, $iMsg
        Local $hWndFrom, $iIDFrom, $iCode
        $hWndFrom = $ilParam
        $iIDFrom = BitAND($iwParam, 0xFFFF) ; Low Word
        $iCode = BitShift($iwParam, 16) ; Hi Word

        Switch $hWndFrom
                
                Case $hInput1

                        Switch $iCode
                                Case $CBN_SETFOCUS
                                        Global $Ctrl_Focus = $hInput1

                        EndSwitch
                        
                Case $hInput2

                        Switch $iCode
                                Case $CBN_SETFOCUS
                                        Global $Ctrl_Focus = $hInput2

                        EndSwitch

        EndSwitch
        Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_COMMAND
;###########################################

Func Dummy_ENTER()
        
        Switch $Ctrl_Focus
                
                Case $hInput1

                Case $hInput2

        EndSwitch

EndFunc   ;==>Dummy_ENTER
;###########################################
发表于 2014-12-12 10:24:39 | 显示全部楼层
回复 2# athland5013


    请问能把代码补全一下吗?
发表于 2014-12-12 12:39:32 | 显示全部楼层
回复 1# sh1536
HotKeySet('{enter}', '_hotkeytest')
$Form1 = GUICreate('test', 380, 180)
$Input1 = GUICtrlCreateInput('Input1', 64, 48, 193, 24)
$Button1 = GUICtrlCreateButton('按钮1', 272, 48, 57, 24)
$Input2 = GUICtrlCreateInput('Input2', 64, 112, 193, 24)
$Button2 = GUICtrlCreateButton('按钮2', 272, 112, 57, 24)
GUISetState()
Do
Until GUIGetMsg() == -3

Func _hotkeytest()
        ;If Not WinActive($Form1) Then Return
        Switch ControlGetFocus($Form1)
                Case 'Edit1'
                        MsgBox(0, '', 'input1')
                Case 'Edit2'
                        MsgBox(0, '', 'input2')
                Case 'Button1'
                        MsgBox(0, '', '按钮1')
                Case 'Button2'
                        MsgBox(0, '', '按钮2')
                Case Else
                        MsgBox(0, '', '窗口未激活')
        EndSwitch
EndFunc
发表于 2014-12-12 13:16:10 | 显示全部楼层
回复 4# user3000


    不错的思路~~ 学习了~
发表于 2014-12-12 18:39:34 | 显示全部楼层
HotKeySet('{enter}', '_hotkeytest')  是全局的

你开了软件之后例如切换到记事本,按个回车都不行,真不好用

还是GUISetAccelerators($HotKeys, $Form1)好点

3000的ControlGetFocus($Form1)的确比较方便
发表于 2014-12-12 18:58:40 | 显示全部楼层
很想告诉你,不过按你的说法,没兴趣了
发表于 2014-12-12 19:00:07 | 显示全部楼层
不过可以提个建议,用bitor
发表于 2014-12-12 19:18:51 | 显示全部楼层
回复 6# athland5013
多谢提醒,代码有欠考虑的地方.现已改正.
Func _hotkeytest()
        If Not WinActive($Form1) Then
                HotKeySet('{enter}')
                Send('{enter}')
                HotKeySet('{enter}', '_hotkeytest')
                Return ;窗口未激活
        EndIf
        ;....................
EndFunc
 楼主| 发表于 2014-12-13 08:47:00 | 显示全部楼层
回复 2# athland5013


    能不能补全啊?我试了一下怎么反应
 楼主| 发表于 2014-12-13 08:51:40 | 显示全部楼层
回复 9# user3000


    谢谢你的回复。不过这样在未激活窗口的时候,无论在那个界面按enter键都会激活窗口,跳出弹窗。我修改了下
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet('{enter}','_HotKeySet')
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 361, 161, 192, 124)
$Input1 = GUICtrlCreateInput("Input1", 72, 40, 121, 21)
$Button1 = GUICtrlCreateButton("Button1", 232, 40, 75, 25)
$Input2 = GUICtrlCreateInput("Input2", 72, 88, 121, 21)
$Button2 = GUICtrlCreateButton("Button2", 232, 88, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Do
Until GUIGetMsg() = -3

Func _HotKeySet()
        If WinGetHandle("") <> HWnd($Form1) Then
                HotKeySet('{enter}')
                Send('{enter}')
                HotKeySet('{enter}','_HotKeySet')
                Return;窗口未激活
        EndIf        
        Switch ControlGetFocus($Form1)
                Case 'Edit1'
                        MsgBox(0,"",'input1')
                Case 'Edit2'
                        MsgBox(0,"",'input2')
                Case 'Button1'
                        MsgBox(0,"",'Button1')
                Case 'Button2'
                        MsgBox(0,"",'Button2')
        EndSwitch
EndFunc
 楼主| 发表于 2014-12-13 08:55:06 | 显示全部楼层
回复 8# netegg


    大大能不能明示啊
 楼主| 发表于 2014-12-13 08:56:01 | 显示全部楼层
其实这里有一篇帖子,跟这个差不多,就是没看懂,发上来你们看看
http://www.autoitx.com/forum.php ... 8%C8%BC%FC%2B%D3%F2
发表于 2014-12-13 16:40:45 | 显示全部楼层
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
Global $Ctrl_Focus

Opt("GUIOnEventMode", 1)

$Form1 = GUICreate("Form1", 361, 161, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Program_Exit")

$Input1 = GUICtrlCreateInput("Input1", 72, 40, 121, 21)
$Button1 = GUICtrlCreateButton("Button1", 232, 40, 75, 25)
$Input2 = GUICtrlCreateInput("Input2", 72, 88, 121, 21)
$Button2 = GUICtrlCreateButton("Button2", 232, 88, 75, 25)


$Dummy = GUICtrlCreateDummy()
GUICtrlSetOnEvent($Dummy, "Dummy_ENTER")

Local $HotKeys[2][2] = [["{Enter}", $Dummy],["{TAB}", $Dummy]]
GUISetAccelerators($HotKeys, $Form1)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        Sleep(10)
WEnd

Func Program_Exit()
        Exit
EndFunc
;###########################################

Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)

        #forceref $hWnd, $iMsg
        Local $hWndFrom, $iIDFrom, $iCode
        $hWndFrom = $ilParam
        $iIDFrom = BitAND($iwParam, 0xFFFF) ; Low Word
        $iCode = BitShift($iwParam, 16) ; Hi Word

        Switch $iIDFrom

                Case $Input1

                        Switch $iCode
                                Case $GUI_FOCUS
                                        Global $Ctrl_Focus = $Input1
                                        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Ctrl_Focus = ' & $Ctrl_Focus & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console


                        EndSwitch

                Case $Input2

                        Switch $iCode
                                Case $GUI_FOCUS
                                        Global $Ctrl_Focus = $Input2
                                        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Ctrl_Focus = ' & $Ctrl_Focus & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console

                        EndSwitch

        EndSwitch
        Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_COMMAND
;###########################################

Func Dummy_ENTER()

        Switch $Ctrl_Focus

                Case $Input1
                        MsgBox(262144, 'Debug line ~' & @ScriptLineNumber, 'Selection:' & @LF & '$Input1' & @LF & @LF & 'Return:' & @LF & $Input1 & @LF & @LF & '@Error:' & @LF & @error) ;### Debug MSGBOX

                Case $Input2
                        MsgBox(262144, 'Debug line ~' & @ScriptLineNumber, 'Selection:' & @LF & '$Input2' & @LF & @LF & 'Return:' & @LF & $Input2 & @LF & @LF & '@Error:' & @LF & @error) ;### Debug MSGBOX

        EndSwitch

EndFunc   ;==>Dummy_ENTER
;###########################################
 楼主| 发表于 2014-12-13 19:01:03 | 显示全部楼层
回复 14# athland5013


    谢谢,又学到了一招!!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-26 15:38 , Processed in 0.085776 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表