• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 590
  • Last Modified:

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.
0
koskia
Asked:
koskia
1 Solution
 
Tommy HuiCommented:
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.
0
 
koskiaAuthor Commented:
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
0
 
koskiaAuthor Commented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jkrCommented:
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.

0
 
koskiaAuthor Commented:
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

0
 
Christ142Commented:
If you want to do that maybe you have to use SetWindowsHookEx( WH_KEYBOARD, .......  )
Try it....
0
 
wuxzCommented:
I think the only way is to write a keyboard hook.
0
 
koskiaAuthor Commented:
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.
0
 
wuxzCommented:
Sorry, map the ALT key to File Menu is not the works by wizard, it's the windows system who do that. So I say that only way is writing a keyboard hook.

I think that there is not other easier way to capture the ALT key, if you have difficulties in writing hook, just speak it out.

Regards
wu xz
0
 
koskiaAuthor Commented:
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
0
 
koskiaAuthor Commented:
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now