[已解决]如何让同一个热键在不同控件获得焦点后执行不同内容?
本帖最后由 sh1536 于 2014-12-13 19:01 编辑大概的情形是这样的:
有Input1,Button1和Input2,Button2,如何在Input1获得焦点后,Enter键就作为Button1的热键,相同的,当Input2获得焦点后Enter键就是Button2的热键。 我的做法是设置个开关,在快捷键响应函数选择,欢迎指正
$Dummy = GUICtrlCreateDummy()
GUICtrlSetOnEvent($Dummy, "Dummy_ENTER")
Local $HotKeys = [["{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
;###########################################
回复 2# athland5013
请问能把代码补全一下吗? 回复 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 回复 4# user3000
不错的思路~~ 学习了~ HotKeySet('{enter}', '_hotkeytest')是全局的
你开了软件之后例如切换到记事本,按个回车都不行,真不好用
还是GUISetAccelerators($HotKeys, $Form1)好点
3000的ControlGetFocus($Form1)的确比较方便 很想告诉你,不过按你的说法,没兴趣了 不过可以提个建议,用bitor 回复 6# athland5013
多谢提醒,代码有欠考虑的地方.现已改正.Func _hotkeytest()
If Not WinActive($Form1) Then
HotKeySet('{enter}')
Send('{enter}')
HotKeySet('{enter}', '_hotkeytest')
Return ;窗口未激活
EndIf
;....................
EndFunc 回复 2# athland5013
能不能补全啊?我试了一下怎么反应 回复 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 回复 8# netegg
大大能不能明示啊 其实这里有一篇帖子,跟这个差不多,就是没看懂,发上来你们看看
http://www.autoitx.com/forum.php?mod=viewthread&tid=20853&highlight=%C8%C8%BC%FC%2B%D3%F2 #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 = [["{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
;###########################################
回复 14# athland5013
谢谢,又学到了一招!!
页:
[1]
2