I'm trying to do something like this but only with the mouse:
require 'win32api'
BlockInput = Win32API.new("user32", "BlockInput", ["I"], "I")
BlockInput.call(1)
sleep 5
BlockInput.call(0)
I'm trying to use SetWindowsHookEx but it say that work but nothing happens, i can continue moving and clicking my mouse, this is the code:
require 'win32/api'
# SetWindowsHookEx CONSTANTS
WH_MOUSE = 7
WH_MOUSE_LL = 14
# MouseProc or LowLevelMouseProc CONSTANTS
WM_MOUSEMOVE = 0x0200
WM_LBUTTONDOWN = 0x0201
WM_LBUTTONUP = 0x0202
WM_MOUSEWHEEL = 0x020A
WM_MOUSEHWHEEL = 0x020E
WM_RBUTTONDOWN = 0x0204
WM_RBUTTONUP = 0x0205
SetWindowsHookEx = Win32::API.new('SetWindowsHookEx', ['I', 'K', 'L', 'I'], 'L', 'user32')
CallNextHookEx = Win32::API.new('CallNextHookEx', ['I', 'I', 'L', 'P'], 'I', 'user32')
UnhookWindowsHookEx = Win32::API.new('UnhookWindowsHookEx', ['I'], 'I', 'user32')
GetCurrentThreadId = Win32::API.new('GetCurrentThreadId', ['V'], 'I', 'kernel32')
MouseProc = Win32::API::Callback.new('ILP', 'L') do |nCode, wParam, lParam|
# https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644988(v=vs.85)
if nCode < 0 # If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
return CallNextHookEx.call(0, nCode, wParam, lParam)
elsif wParam == WM_MOUSEMOVE or wParam == WM_LBUTTONDOWN or wParam == WM_LBUTTONUP
# If the hook procedure processed the message, it may return a nonzero value to prevent the system from
# passing the message to the rest of the hook chain or the target window procedure.
return 1
else
# If nCode is greater than or equal to zero, and the hook procedure did not process the message,
# it is highly recommended that you call CallNextHookEx and return the value it returns;
# otherwise, other applications that have installed WH_MOUSE_LL hooks will not receive hook
# notifications and may behave incorrectly as a result.
return CallNextHookEx.call(0, nCode, wParam, lParam)
end
end
threadID = GetCurrentThreadId.call
hMod = nil
# If the function succeeds, the return value is the handle to the hook procedure.
p hhk = SetWindowsHookEx.call(WH_MOUSE, MouseProc, hMod, threadID) #=> 166134929 (Good)
# But nothing happens, i can continue moving and clicking my mouse.
sleep 5
p UnhookWindowsHookEx.call(hhk) #=> 1 (Good)
if someone knows how to do it or how to do it in another way I would greatly appreciate it.
Edit: I'm running the code with admin access on Windows 7 (32 Bits)
I'm trying to use GetMessage() following Strive Sun - MSFT's answer but i have some problems passing MSG pointer
GetMessage = Win32::API.new('GetMessage', ['P', 'L', 'L', 'L'], 'I', 'user32')
point = [0, 0].pack('LL')
hwnd = uint = wparam = lparam = dword = 0
buf = [hwnd, uint, wparam, lparam, dword, point, dword].pack('JIL2Sa8S')
# J > hwnd, I > uint, L2 > wparam lparam, S > dword, a8 > point
GetMessage.call(buf, nil, 0, 0) #=> Stuck, i need to use Manager Task to close it.