Solved

Capturing The Alt key

Posted on 1999-01-16
11
583 Views
Last Modified: 2013-11-20
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
Comment
Question by:koskia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1327789
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
 

Author Comment

by:koskia
ID: 1327790
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
 

Author Comment

by:koskia
ID: 1327791
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 1327792
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
 

Author Comment

by:koskia
ID: 1327793
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
 

Expert Comment

by:Christ142
ID: 1327794
If you want to do that maybe you have to use SetWindowsHookEx( WH_KEYBOARD, .......  )
Try it....
0
 
LVL 2

Expert Comment

by:wuxz
ID: 1327795
I think the only way is to write a keyboard hook.
0
 

Author Comment

by:koskia
ID: 1327796
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
 
LVL 2

Accepted Solution

by:
wuxz earned 130 total points
ID: 1327797
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
 

Author Comment

by:koskia
ID: 1327798
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
 

Author Comment

by:koskia
ID: 1327799
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question