Link to home
Start Free TrialLog in
Avatar of koskia
koskia

asked on

Capturing The Alt key

hey all!

all I need is a way of mapping pressing the alt key, and the alt key only.
I need to map both pressing the alt down and releasing the alt up.
I need the cursor to change if I'm moving pressing the alt, and change back when releasing.

thanks alot,
btw, capturing the control alone will be also a big help, but the alt is the main problem for me.
Avatar of Tommy Hui
Tommy Hui

The easiest way is to trap WM_KEYDOWN and/or WM_KEYUP for VK_MENU. Then also check the message flag for KF_ALTDOWN to see if the ALT key was responsible for sending the VK_MENU. Note that the F10 key can also be the one sending the VK_MENU message.
Avatar of koskia

ASKER

Sorry for rejecting your answer,
notice that, I WILL ACCEPT IT AFTER SOME MORE EXPLANATIONS

the one who've worked before me on this project used the wizard, and the ALT key is mapped to open the menu, actually it sends a OnSysCommand message along with the SC_KEYMENU flag.

when doing what you suggesting, nothing happens, and actually the menu message continues.
how can I stop the ALT key being mapped to the menu, and make it go to OnKeyDown() ???

appreciatte your help
Asaf
Avatar of koskia

ASKER

Sorry for rejecting your answer,
notice that, I WILL ACCEPT IT AFTER SOME MORE EXPLANATIONS

the one who've worked before me on this project used the wizard, and the ALT key is mapped to open the menu, actually it sends a OnSysCommand message along with the SC_KEYMENU flag.

when doing what you suggesting, nothing happens, and actually the menu message continues.
how can I stop the ALT key being mapped to the menu, and make it go to OnKeyDown() ???

appreciatte your help
Asaf
Avatar of jkr
You can alway monitor the state of any key by calling 'GetKeyState()' when receiving a WM_KEYDOWN' or 'WM_KEYUP'  msg, e.g.
if (0x80000000 & GetKeyState(VK_CONTROL)) // CTRL is pressed
0x80000000 & GetKeyState(VK_MENU)  // ALT key is pressed
Thie bitmask for detecting the release of the key is slightly different, if the high order bit is set (as above), the key is down, otherwise not.

Avatar of koskia

ASKER

Rejecting, Rejecting,
did you ever get off with this short answers ??!!

there's a problem with what you're suggesting, because of 2 reasons, and if you read my first rejection, you would see that.
first:
      I'll repeat. because someone else started this project,
      when the alt key is pressed, there is no WM_KEYDOWN message
      I've ran with the SPY++ and so that only WM_SYSCOMMAND
      message is sent and the function don't even starts.
      It's a menu call (as in most windows App, Alt goes to File)
      WM_SYSCOMMAND sends a notice the ALT was pressed, but in a
      single handed way (Like a button click and not down and up)

second:
      even if I was able to enter the message, the mask above
      don't require the demand. because the ALT can be pressed
      along with any other key.
      how do I check every key?? the first answer seems better,
      but I've already knew that.

How do I monitor the ALT press down and up key to WM_LBUTTONDOWN, and WM_LBUTTONUP ????
again, if the ALT is already mapped (by the wizard) to the menus above, and only sends WM_SYSCOMMAND messages.

p.s.
This is a 150 files project and I can't simply run the wizard again and move the files, besides I need menus too, just want to monitor the ALT key down and up myself.

thanks,
    asaf

If you want to do that maybe you have to use SetWindowsHookEx( WH_KEYBOARD, .......  )
Try it....
I think the only way is to write a keyboard hook.
Avatar of koskia

ASKER

you all do this on purpose,
these short answers are bad answers, and read my comments before you're writing a single insulting lines.
I rather have the question keep resides in the 'open questions' section, rather than accepting a question which have only one line and starts with if.

I'll settle anyway with a solution of how you unmap the ALT key, which the wizard mapped to the File Menu on a standard project.
ASKER CERTIFIED SOLUTION
Avatar of wuxz
wuxz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of koskia

ASKER

well, I'll except that's the way it is,
Havn't written a hook before, and still have a little trouble,
I'll except your answer, but please be kind to shad some light on this wrong code.

LRESULT CALLBACK LowLevelKeyboardProc(  int nCode,     // hook code
  WPARAM wParam, // message identifier
  LPARAM lParam  /* pointer to structure with message data*/)

{
     CMainFrame* m_pFrame =
    (CMainFrame*)AfxGetApp()->m_pMainWnd;
    ASSERT(m_pFrame);
    CApplicationView* pView =
   (CApplicationView*)m_pFrame->GetActiveView();
   ASSERT(pView);

   if (wParam == VK_MENU)
   {
        if (lParam & KF_UP)
        {
           ChangeTool(Tool1);
           pView->SetCursor(pView, HTCLIENT, 0);
           TRACE("Alt Pressed\n");
        }
        else
        {
            ChangeTool(Tool2);
            pView->SetCursor(pView, HTCLIENT, 0);
            TRACE("Alt Released\n");
         }
          return TRUE;
    }

      return CallNextHookEx(m_pFrame->HookHandle,
      nCode, wParam, lParam);
}

here's the last problem, I'm trying to figure out how can I differ between WM_KEYDOWN and WM_KEYUP, and I aint able to do it.

I've also build the mask myself, it never gets to the else part of the equation.
even tried to check lParam with the debugger, but :))
if I run the debugger it only gets 1 time into the hook callback function.

any ideas?, I'll appreciate if you'll continue to help me on this little matter.

asaf
Avatar of koskia

ASKER

solved it, thanks anyway.

int mask = 1;
mask <<= 31;

if (lParam & mask)
 ...
else
 ...

works, I've made the wrong mask earlier,
thanks for all your help, here from you soon.

asaf