Solved

Capturing The Alt key

Posted on 1999-01-16
11
576 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Detect CR LF to each line 12 152
no14 challenge 14 62
Execute multiple curl cmds with sleep and send output to file 10 88
Annoying "thing" blocks my view 4 81
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now