473,799 Members | 3,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OnKeyDown and integration with managed code

Hello,

I have a legacy MFC application that has a lot of editor functionality
associated with a class that extends CFrameWnd. This editor watches for key
down events by using the OnKeyDown handler in the message map. In my legacy
MFC application, my OnKeyDown handler is always called, as I would expect.

I'm calling the legacy application's editor window from within a C#
application. I've written a little C++/CLI application that bridges the
managed code, and exposes a C# friendly method for via explicit p/invoke.
This allows my editor to show up and (generally) work as I'd hope.

The problem is that the OnKeyDown handler in the CFrameWnd based dialog does
not always get called when the window is invoked from my managed code. It
gets called when I use simple keys like 'A' or '1' but if I press the Esc
key, my handler is not called.

Can anyone give any clues as to why my handler is not being called when the
Esc key is pressed? I used Spy++ to look at the messages being sent to the
CFrameWnd based dialog and it is receiving the WM_KEYDOWN message with the
VK_ESCAPE virtual key. Also, in case it matters, in my legacy application,
the window that launches the CFrameWnd based dialog is modeless, while in my
C# application, the window from which the CFrameWnd based dialog is launched
is modal.

Thanks!
Notre
Feb 13 '07 #1
10 2529

"Notre Poubelle" <no************ @online.nospamw rote in message
news:EF******** *************** ***********@mic rosoft.com...
Hello,

I have a legacy MFC application that has a lot of editor functionality
associated with a class that extends CFrameWnd. This editor watches for
key
down events by using the OnKeyDown handler in the message map. In my
legacy
MFC application, my OnKeyDown handler is always called, as I would expect.

I'm calling the legacy application's editor window from within a C#
application. I've written a little C++/CLI application that bridges the
managed code, and exposes a C# friendly method for via explicit p/invoke.
This allows my editor to show up and (generally) work as I'd hope.

The problem is that the OnKeyDown handler in the CFrameWnd based dialog
does
not always get called when the window is invoked from my managed code. It
gets called when I use simple keys like 'A' or '1' but if I press the Esc
key, my handler is not called.

Can anyone give any clues as to why my handler is not being called when
the
Esc key is pressed? I used Spy++ to look at the messages being sent to
the
CFrameWnd based dialog and it is receiving the WM_KEYDOWN message with the
VK_ESCAPE virtual key. Also, in case it matters, in my legacy
application,
the window that launches the CFrameWnd based dialog is modeless, while in
my
C# application, the window from which the CFrameWnd based dialog is
launched
is modal.
The .NET local message dispatch loop, used for modal windows, probably calls
IsDialogMessage and then doesn't dispatch the message through your window
procedure. Try Application.Add MessageFilter for a workaround (catch
WM_KEYDOWN, call TranslateMessag e+DispatchMessa ge, and then kill the
message, let all other messages be default processed).
>
Thanks!
Notre

Feb 13 '07 #2
Hi Notre,

Yes, just as Ben pointed out, .Net Winform encapsulates the GUI message
loop in the .Net BCL code. Also, to support some special key function in
..Net Winform, the WndProc of .Net Winform code may implement some type of
preprocessing to the message before dispatching to your OnKeyDown handler.
You may use Reflector to view the source code of
System.Windows. Forms.Applicati on.ThreadContex t.PreTranslateM essage method
to get a glance of the preprocessing.

So I suspect the .Net Winform preprocessing code may identify the ESC key
as a special key and did not dispatch to the MFC handler correctly.

Is it possible for you to create a little sample C++/CLI MFC project to
help me reproduce this problem? Once I can give it a local reproduce, I
will perform some debugging over it and provide some suggestion to you.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.
Feb 13 '07 #3
Thank you both Ben & Jeffrey for your prompt responses!

I'm not sure if I'm stating the problem correctly, or maybe I just do not
understand the responses.

The editor that extends CFrameWnd is the one that is not recognizing the
OnKeyDown handler when the Esc key is pressed. This editor is itself a
modeless window. It is the 'parent' window that is different between my
managed application and my legacy MFC application. In my managed
application, this parent window is modal. In my legacy application, this
parent window is modeless.

Do you guys think that your comments still apply, or are they more if the
editor window itself (the one that extends CFrameWnd) had changed from a
modeless to a modal window?

Thanks again!!!
Notre
Feb 13 '07 #4

"Notre Poubelle" <no************ @online.nospamw rote in message
news:82******** *************** ***********@mic rosoft.com...
Thank you both Ben & Jeffrey for your prompt responses!

I'm not sure if I'm stating the problem correctly, or maybe I just do not
understand the responses.

The editor that extends CFrameWnd is the one that is not recognizing the
OnKeyDown handler when the Esc key is pressed. This editor is itself a
modeless window. It is the 'parent' window that is different between my
managed application and my legacy MFC application. In my managed
application, this parent window is modal. In my legacy application, this
parent window is modeless.

Do you guys think that your comments still apply, or are they more if the
editor window itself (the one that extends CFrameWnd) had changed from a
modeless to a modal window?
If any modal window exists on the thread, then message processing will be
done through a local dialog message dispatch loop instead of the main
application event loop. In Win32, such a loop calls IsDialogMessage before
dispatching messages, I see that the method Jeffrey mentioned inside .NET
does as well
(System.Windows .Forms.Applicat ion+ThreadConte xt.LocalModalMe ssageLoop(Form)
calls
System.Windows. Forms.Applicati on+ThreadContex t.PreTranslateM essage(ref MSG)
pinvokes IsDialogMessage ). IsDialogMessage is known to eat the Escape key.

However, before calling IsDialogMessage , PreTranslateMes sage calls
ProcessFilters, which invokes registered IMessageFilter objects, hence I
suggested that you use Application.Add MessageFilter to register a message
filter, which can pinvoke TranslateMessag e and DispatchMessage , as
LocalModalMessa geLoop does for all messages not processed by
IsDialogMessage . You should also check that the destination hwnd of the
message is your editor window or one of its descendants, otherwise you may
break the behavior of the owner modal window.
>
Thanks again!!!
Notre

Feb 13 '07 #5
Ben, this worked great! Thanks a lot!

Notre
Feb 14 '07 #6
Hi Notre,

Glad to see Ben's suggestion of using IMessageFilter resolves your problem.

If you are curious, I have discussed this issue with one Architect of the
VC++ team. We agree that the modal issue is likely the problem. See the KB
below:
"PRB: Modal Dialog Box Prevents Calls to PreTranslateMes sage"
http://support.microsoft.com/kb/126874.

Below is his comment:

"This KB isn't 100% correct in my opinion, however. I added code to MFC
back in '96 /' 97 to address this problem with normal modal MFC dialogs.
MFC has its own modal message loop now that does all the correct MFC things
like calling PreTranslateMes sage.

However, if you are hosted by something else, this isn't going to happen.
The ESC key is normally used to cancel a modal dialog and this won't be
passed to any control in a dialog normally.

Exactly how is this dialog being hosted and invoked? Who actually controls
(i.e. who runs the loop of) the top-level dialog window? A screen shot
from Spy++ with the window hierarchy shown would be helpful."

Anyway, since your problem is resolved, hope this information just makes
sense to you.

If you need further help, please feel free to post. I am glad to help you.
Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.
Feb 14 '07 #7
Thank you Jeffrey for following up further on this, and speaking with an
arhitect about the problem. I read through the KB and the architect's
comments for my own education. Ben's solution does indeed work, and I think
proves that the modal dialog box was causing the problem.

Thanks again,
Notre

Feb 14 '07 #8

"Notre Poubelle" <no************ @online.nospamw rote in message
news:E0******** *************** ***********@mic rosoft.com...
Ben, this worked great! Thanks a lot!
Glad it worked for you.
Feb 14 '07 #9
Hi Notre,

Thanks for your feedback.

Glad to see the architect's reply makes sense to you. Yes, I also learned a
lot during the communication between the customer and the VC++ product
team. :-)

If you need further help, please feel free to post, thanks!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.
Feb 15 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3913
by: Z | last post by:
I have sub-classed the TextBox. In its OnKeyDown event I can intercept key strokes, examine them, etc. When I get a certain keycode (e.g., 'A') I want to change it to another unicode key from a different code page (e.g., "\u2801"). But the KeyCode, KeyData, and KeyValue properties of KeyEventArgs are all only getters. They are read only and thus do not permit changing the assigned KeyCode. My question is this: How do I change the KeyCode...
10
9174
by: b.dam | last post by:
I'm trying the following: function grid() { this._el = document.createElement("TABLE"); var self = this; document.addEventListener("onkeydown", function(event) {self.gridKeyDown(event);}, false); }
2
5959
by: Iver Erling Årva | last post by:
I have come across a problem with the onKeyDown event in some of my forms. I'm using onKeyDown in <form> as a standard method to open my help screen system throughout my system, but I have discovered that If I have a <div></div> section somewhere and then load the contents of it from another file using innerHTML after the main window is loaded, the onKeyDown event doesn't trigger any more. I'm using IE6 and the structure is: <body...
5
3786
by: Roger Withnell | last post by:
I'm using the following code to start a function on key down. document.onkeydown = OnKeyDown; function OnKeyDown() { vKeyCode = event.keyCode; ..code... }
0
1147
by: =?Utf-8?B?Q0dX?= | last post by:
I have a .NET 1.1 application which uses several grids of text boxs (in repeaters and datagrids) for entering arrays of time values. I use onkeydown to allow users to navigate through the grids using arrow keys. I'm attempting to upgrade to .NET 2.0, but that particular functionality appears to be broken now. The actual javascript function is quite long and really irrelevant since the problem appears to be with the onkeydown event firing...
0
1196
by: CGW | last post by:
I posted this in a .NET general newsgroup, but then found this group and I think it would be more appropriately posted here. Even though the problem is partially with client side behavior, I'm attempting to deal with it in my VB code behind. Here's my post... I have a .NET 1.1 application which uses several grids of text boxs (in repeaters and datagrids) for entering arrays of time values. I use onkeydown to allow users to navigate...
3
3618
by: 2b|!2b==? | last post by:
Die-hard C++ enthusiast hear (only been recently converted - actually dragged kicking and screaming to ASP.net). I have loads of C++ libraries - what is the technology/methodolgy that provides the tightest integration of C++ and the .Net languages (C# especially - since that is my target language) BTW, preference is given to methodologies/technologies that involve the least learning curve (from a seasoned C++ programmers point of view).
1
5761
eboyjr14
by: eboyjr14 | last post by:
I have this UserScript for Grease monkey. but I can't get the onleydown event to fire in FIREFOX only. I've looked everywhere! // ==UserScript== // @name iGoogle Suggest // @namespace Devin Samarin // @description This automatcally selects the top result for your query. // @include *google.com/ig* // ==/UserScript== function makeRequest() {
8
4274
by: Tony Johansson | last post by:
Hello! I wonder can somebody explain when is it suitable to use these methods OnKeyUp, OnKeyDown and OnKeyPress because these raise an event. These are located in class UserControl. If these raise an event how do I create an handler to catch these raised events. //Tony
0
10259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.