Solved

Capturing The Alt key

Posted on 1999-01-16
11
572 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

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

Expert Comment

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

Author Comment

by:koskia
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

11 Experts available now in Live!

Get 1:1 Help Now