屏幕取词鼠标钩子【已解决】
本帖最后由 lsszmj 于 2020-5-21 09:45 编辑最近想做一个屏幕取词在线翻译小工具,其中有关屏幕取词使用最多是鼠标钩子,对于我等小白只能网络搜索,特别感谢afan大神提示“鼠标弹起”。目前遇到了一些问题,希望得到各位大神的帮助。1.我拼凑的鼠标钩子
Global Const $WH_MOUSE_LL = 14
Global Const $WM_LBUTTONUP = 0x0202
#cs
https://docs.microsoft.com/zh-cn/previous-versions/windows/desktop/legacy/ms644986(v=vs.85)
LRESULT CALLBACK LowLevelMouseProc(
_In_ int nCode,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);
#ce
Local $hCallback = DllCallbackRegister("_LowLevelMouseProc", "int", "int;WPARAM;LPARAM")
Local $hModule = _WinAPI_GetModuleHandle(0)
;安装一个钩子程序, 监控鼠标消息
Local $hHook = DllCall("user32.dll", "handle", "SetWindowsHookEx", "int", $WH_MOUSE_LL, "ptr", DllCallbackGetPtr($hCallback), "handle", $hModule, "dword", 0)
OnAutoItExitRegister("_Clean")
While 1
Sleep(10)
WEnd
Func _LowLevelMouseProc($nCode, $wParam, $lParam)
If $nCode < 0 Then Return DllCall("user32.dll", "lresult", "CallNextHookEx", "handle", $hHook, "int", $nCode, "wparam", $wParam, "lparam", $lParam)
Switch $wParam
Case $WM_LBUTTONUP;鼠标左键抬起
Send("^c")
EndSwitch
Return DllCall("user32.dll", "lresult", "CallNextHookEx", "handle", 0, "int", $nCode, "wparam", $wParam, "lparam", $lParam)
EndFunc ;==>_LowLevelMouseProc
Func _Clean()
DllCall("user32.dll", "bool", "UnhookWindowsHookEx", "handle", $hHook)
DllCallbackFree($hCallback)
EndFunc
Func _WinAPI_GetModuleHandle($sModuleName)
Local $sModuleNameType = "wstr"
If $sModuleName = "" Then
$sModuleName = 0
$sModuleNameType = "ptr"
EndIf
Local $aResult = DllCall("kernel32.dll", "handle", "GetModuleHandleW", $sModuleNameType, $sModuleName)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult
EndFunc ;==>_WinAPI_GetModuleHandle2.遇到的问题
只能复制网页中的选中的内容。WORD,excel,记事本里的都获取不了,测试系统 win10。
3.希望得到各位大神的帮助,再次感谢!
你这是划词啊,取词是应该是鼠标悬停就自动取,不过这两样我一样也不会。 最好不要在钩子事件内处理 send 这类操作,可简单赋值一个变量以在外部处理,尽早返回 a大,可以帮忙给段代码,谢谢 lsszmj 发表于 2020-5-20 21:48
a大,可以帮忙给段代码,谢谢
方法肯定有很多,举个例子
Global Const $WH_MOUSE_LL = 14
Global Const $WM_LBUTTONUP = 0x0202
#cs
https://docs.microsoft.com/zh-cn/previous-versions/windows/desktop/legacy/ms644986(v=vs.85)
LRESULT CALLBACK LowLevelMouseProc(
_In_ int nCode,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);
#ce
Local $hCallback = DllCallbackRegister("_LowLevelMouseProc", "int", "int;WPARAM;LPARAM")
Local $hModule = _WinAPI_GetModuleHandle(0)
;安装一个钩子程序, 监控鼠标消息
Local $hHook = DllCall("user32.dll", "handle", "SetWindowsHookEx", "int", $WH_MOUSE_LL, "ptr", DllCallbackGetPtr($hCallback), "handle", $hModule, "dword", 0)
OnAutoItExitRegister("_Clean")
Global $bIsLBUTTONUP
While 1
Sleep(10)
If $bIsLBUTTONUP Then
Send("^c")
$bIsLBUTTONUP = False
EndIf
WEnd
Func _LowLevelMouseProc($nCode, $wParam, $lParam)
If $nCode < 0 Then Return DllCall("user32.dll", "lresult", "CallNextHookEx", "handle", $hHook, "int", $nCode, "wparam", $wParam, "lparam", $lParam)
Switch $wParam
Case $WM_LBUTTONUP;鼠标左键抬起
;~ Send("^c")
$bIsLBUTTONUP = True
EndSwitch
Return DllCall("user32.dll", "lresult", "CallNextHookEx", "handle", 0, "int", $nCode, "wparam", $wParam, "lparam", $lParam)
EndFunc ;==>_LowLevelMouseProc
Func _Clean()
DllCall("user32.dll", "bool", "UnhookWindowsHookEx", "handle", $hHook)
DllCallbackFree($hCallback)
EndFunc ;==>_Clean
Func _WinAPI_GetModuleHandle($sModuleName)
Local $sModuleNameType = "wstr"
If $sModuleName = "" Then
$sModuleName = 0
$sModuleNameType = "ptr"
EndIf
Local $aResult = DllCall("kernel32.dll", "handle", "GetModuleHandleW", $sModuleNameType, $sModuleName)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult
EndFunc ;==>_WinAPI_GetModuleHandle
afan 发表于 2020-5-20 22:01
方法肯定有很多,举个例子
再次感谢A大
页:
[1]