Hi I need help with this hook I am learning about hooks and I am doing one easy example to learn more about setwindowshookex of microsoft.
The first thing I am trying to include different control keys like ctrl, enter, etc I have tried this code:
if( wParam == WM_SYSKEYDOWN ){
switch (wParam)
{
case VK_SHIFT:
qDebug() << "prueba";
break;
default:
break;
}
}
Obviously didnt work. So i can do comparing but i prefer to include some type to check control key separated. At the same i am trying to include something to separate when i using different applications like notepad for learning.
The normal way you check for modifier keys in a keyboard hook function is using GetKeyState, e.g.
LRESULT DLL_CALL KeyboardFunc (int nCode, WPARAM wParam, LPARAM lParam)
{
...
if (nCode < 0)
return (CallNextHookEx (hKeyHook, nCode, wParam, lParam));
if (nCode == HC_NOREMOVE)
return (CallNextHookEx (hKeyHook, nCode, wParam, lParam));
switch (wParam)
{
case VK_F12 :
// Check for modifier keys:
if ((GetKeyState (VK_CONTROL) & iHiOrder) && // ctrl key
(GetKeyState (VK_SHIFT) & iHiOrder) && // shift key
(GetKeyState (VK_MENU) & iHiOrder) // Alt key
{
}
where HiOrder is a const SHORT value with the high-order bit set.
Related
I want to learn something about Windows Hooks. Right now, I'm only interested in "catching" messages.
So I did the following, but it doesn't work.
I want to catch the message in the same thread I'm using. I don't want to catch another thread's messages.
Can somebody explain to me what I'm doing wrong?
I install the hook as follows:
myhookdata.nType = WH_GETMESSAGE;
myhookdata.hkprc = GetMsgProc;
myhookdata.hhook = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, NULL, GetCurrentThreadId());
Then in the hook procedure I do this, just for testing. But the "WM_LBUTTONDOWN" never get catched!!
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
LPCWPSTRUCT message = (LPCWPSTRUCT)lParam;
if (nCode < 0)
return CallNextHookEx(myhookdata.hhook, nCode,
wParam, lParam);
switch (nCode)
{
case HC_ACTION:
if (wParam)
if (message->message == WM_LBUTTONDOWN)
Sleep(0);
break;
default:
break;
}
return CallNextHookEx(myhookdata.hhook, nCode, wParam, lParam);
}
According to the documentation:
lParam [in]
Type: LPARAM
A pointer to an MSG structure that contains details about the message.
So you only need to change the code:
PMSG message = (PMSG)lParam;
Then it works for me.
I have successfully caught Alt+F4 inside my overridden wndproc function using:
LRESULT CALLBACK NewWndProc(HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
if (uMsg == WM_SYSKEYDOWN)
{
switch (wParam)
{
case VK_F4:
{
qDebug() << "Alt+F4 pressed";
break;
}
};
}
else
return CallWindowProc(OldWinProc,hwnd,uMsg,wParam,lParam);
}
I now need to catch Ctrl+Alt+Tab and Ctrl+Alt+←
But I can't seem to find them.
I know in C# one of the parameters in the function is:
Keys keyData
And I can use the following:
case Keys.Control | Keys.Alt | Keys.Q:
How do I get those key combinations in C++?
Use GetKeyState WinAPI function to get state of modifier keys like Ctrl. For example:
case VK_LEFT:
{
if (GetKeyState(VK_CONTROL) & 0x8000)
{
qDebug() << "Alt+Ctrl+Left Arrow pressed";
break;
}
}
But please take in account that some videocard drivers under Windows can rotate screen on Ctrl+Alt+← combination and intercept this combination before your code gets it. And it will be a bit hard to implement getting this keyboard event before driver. However you can disable this feature
I have a procedure that when a user press Ctrl button and right click it will show a message box to screen. But it has a loop, I only press Ctrl button and right click once time but it shows a sequence of message box. How to fix this?
https://youtu.be/LzI9M_zEEKQ
This is my MouseProc procedure
#define EXPORT __declspec(dllexport)
unsigned char KeyState[256];
LRESULT EXPORT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0)
return CallNextHookEx(hHook, nCode, wParam, lParam);
GetKeyboardState(KeyState);
if (nCode == HC_ACTION)
{
if ((wParam == WM_RBUTTONUP) && (KeyState[VK_CONTROL] & 0x80))
{
MessageBox(NULL, L"Ctrl + Right Click", L"Mouse hook", MB_OK);
}
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
Thanks for reading.
You shall not use MessageBox() in message hooks as it breaks normal message hook flow - MessageBox() runs its own modal loop.
If you need exactly MessageBox there then you should use PostMessage with custom message and handler. In this case MessageBox will be invoked after CallNextHookEx(hHook, nCode, wParam, lParam);
Ello All,
I'm pretty new to c++ and I've been trying to get this to work for longer than I care to admit. So I've gone off of the following refs and gotten the controller to work in a console app.
xbox360 controller input with c using x input
Code proj article
MSDN
Here is the result
xbox360Controller.h
xbox360Controller.cpp
From there I'm trying to get it to work with cocos2d-x using steve tranby's post at the bottom of this thread (he adds to the )and adapting that to the 360 gamepad.
While I've gotten the keypad events to work
(it was standard windows input so not too bad)
LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
BOOL bProcessed = FALSE;
CCLog("Message sent = %d",message);
//note* only showing relavant sections of code for brevity
switch (message)
{
case WM_KEYDOWN:
if (wParam == VK_F1 || wParam == VK_F2)
{
CCDirector* pDirector = CCDirector::sharedDirector();
if (GetKeyState(VK_LSHIFT) getKeypadDispatcher()->dispatchKeypadMSG(wParam == VK_F1 ? kTypeBackClicked : kTypeMenuClicked);
}
}
else if (wParam == VK_ESCAPE)
{
CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadMSG(kTypeBackClicked);
}
else
{
CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadDown(wParam);
}
if ( m_lpfnAccelerometerKeyHook!=NULL )
{
(*m_lpfnAccelerometerKeyHook)( message,wParam,lParam );
}
break;
default:
if (m_wndproc)
{
m_wndproc(message, wParam, lParam, &bProcessed);
if (bProcessed) break;
}
return DefWindowProc(m_hWnd, message, wParam, lParam);
}
if (m_wndproc && !bProcessed)
{
m_wndproc(message, wParam, lParam, &bProcessed);
}
return 0;
}
I can't figure out where to put the controller logic. I've tried
in the WindowProc method and realized that didnt work as it only
fires as a callback to WindowProc events(I probably don't have the correct lingo for it, sorry)
The closest I've gotten it to something that fires often enough to check is at the point
static LRESULT CALLBACK _WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CCDirector* pDirector = CCDirector::sharedDirector();
XboxController* player1 = new XboxController(GamePadIndex_One);
if(player1->IsConnected())
{
player1->Update();
for(int i =0;iState._buttons[i]==true)
{
//CCApplication::sharedApplication()->getKe
pDirector->getKeypadDispatcher()->dispatchKeypadDown(i);
}
}
}
delete player1;
if (s_pMainWindow && s_pMainWindow->getHWnd() == hWnd)
{
return s_pMainWindow->WindowProc(uMsg, wParam, lParam);
}
else
{
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
The full code is here
and the source is at github Here.
Anyone know the right place to put the XboxController instance in order for it to respond to KeypadDown properly?
First of, WindowProc is a callback function that is called after there's an message sent to the window from Windows.
So, if there's no message, this function would not get called.
XInput api is not message-based api, it does not generate messages. It requires the app to read its state as frequent as possible. Usually it's read in every game loop, just before the game logic is processed. Alternatively, you could have a separated thread to poll the state every 33ms or so.
I'd to recommend you to take a look at how XInput work, the concept of Windows programming, and the basic concept of game engine architecture.
my program knows 2 states: A annd B, where A is the default state.
When you press ALT-# it should change it's state to B and when you release the combination it should go back to A.
This should work with a LowLevel Keyboard hook but it seems I am stuck somewhere.
#define VK_POUND 0xBF // 191 - the # key
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
KBDLLHOOKSTRUCT* kbdStruct = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);
switch (wParam) {
case WM_KEYDOWN: {
std::cout << kbdStruct->vkCode << "\n";
if (kbdStruct->vkCode == VK_POUND && kbdStruct->flags & LLKHF_ALTDOWN) {
MessageBox(NULL, "WE GOT IT", "", MB_OK);
}
} break;
case WM_KEYUP: {
} break;
}
}
return CallNextHookEx(g_HotKeyHook, nCode, wParam, lParam);
}
What confuses me: When I press the combination ALT and # there is no output in the console.