English 中文(简体)
Equivalent of PreviewKeyDown of C# needed for native C++/MFC
原标题:

I used a ActiveXControl in a C# Forms application and had the possibility to implement a PreviewKeyDown event handler for that ActiveXControl. That was needed to specify that, e.g. the [ALT] key, is an input key.

For some reasons or other I have to reimplement the application in native C++/MFC and don t know how to specify that this [ALT] key is an input key and to be handled by the ActiveXControl.

问题回答

You could use SetTimer to generate a WM_TIMER event 20 times a second or so

SetTimer( NULL, kMyTimer, 50, MyTimerCallback );

Then implement a function as follows.

 void CALLBACK MyTimerCallback( HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime )
 {
      static short lastLeftAltPress = 0;
      short thisLeftAltPress = GetAsyncKeyState( VK_LMENU );
      if ( thisLeftAltPress != 0 && lastLeftAltPress == 0 )
      {
           CallAltHandlingCode();
      }
      thisLeftAltPress = lastLestAltPress;

      // Handling code for other keys goes here.
 }

This will then poll the keyboard every 50ms to find out if the left alt key has just been pressed and then call your handling code. If you want to fire the handler when its released then you would use the following if statement

 if ( thisLeftAltPress == 0 && lastLeftAltPress != 0 )

or if you just want to see if it is down then you do

 if ( thisLeftAltPress != 0 )

The docs for GetAsyncKeyState do state that you can check whether the lowest bit is set to see if the key has just been pressed but it does also point out that this may fail in unexpected ways in multi-threaded environments. The above scheme should always work.

thanks for all your recommendations.

For my case I finally found some way how to make it work. So far what I have learnt the situation is that a PreviewKeyDown event is triggered before the KeyDown message is sent.

I implemented PreTranslateMessage in the dialog which hosts the ActiveX control and called ::SendMessage(WM_KEYDOWN...) with the handle of the ActiveX control, using pMsg for the arguments.

Cheers,

Ilmari

I think that you can use the GetAsyncKeyState Win32 function (http://msdn.microsoft.com/en-us/library/ms646293%28VS.85%29.aspx) to get the state of all the keys in the system, VK_MENU (http://msdn.microsoft.com/en-us/library/dd375731%28v=vs.85%29.aspx) is the key you should monitor for the [Alt] key.

Haven t tried this, but MSDN doesn t say that you won t get a WM_KEYDOWN for the ALT key -- any other key pressed while ALT is down generates a WM_SYSKEYDOWN, but that doesn t say that you can t handle ALT via WM_KEYDOWN.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签