RE: How to override RichTextBox ProcessCndKey() for text cursor contro
- From: v-jetan@xxxxxxxxxxxxxxxxxxxx ("Jeffrey Tan[MSFT]")
- Date: Thu, 14 Jul 2005 06:16:07 GMT
Hi Dave,
Thanks for your post.
I have tried your code snippet and reproduced out your problem.
Actually, ProcessDialogKey method only processes the dialog characters,
such as TAB, RETURN, ESCAPE, and arrow keys(for keyboard navigation). It is
not the end processor for any keyboard key processing. So it works for our
original TAB key, but does not work for the down/up key.(It does nothing
for these 2 keys)
Can we use some other winform methods to simulate down/up key? No, because
in this sitution, the shift+down/up behavior is handled by underlying
RichEdit window procedure, not handled by .Net Winform code.(Still remember
that Tab and shift+tab is handled by .Net Winform code, not win32 code)
Also, if you are fimiliar with Win32 keyboard processing you will know
that, the shift modifier key is not passed as certain parameter of the
WM_KEYDOWN message, while it is stored in keyboard input-state table. And
in window procedure of RichEdit control, it will determine if the shift key
is holded with querying this table. So in order to provide different
behavior to the RichTextBox control, we can just clear shift key state in
keyboard input-state table. We can p/invoke GetKeyboardState and
SetKeyboardState functions to get this done, below is the sample code
snippet:
public class MyRichTextBox : System.Windows.Forms.RichTextBox
{
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern int GetKeyboardState(byte[] keystate);
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern int SetKeyboardState(byte[] keystate);
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ( keyData == ( Keys.Shift | Keys.Up ) )
{
byte[] buffer1 = new byte[0x100];
GetKeyboardState(buffer1);
buffer1[0x10] = 0;
SetKeyboardState(buffer1);
}
else if ( keyData == ( Keys.Shift | Keys.Down ) )
{
byte[] buffer1 = new byte[0x100];
GetKeyboardState(buffer1);
buffer1[0x10] = 0;
SetKeyboardState(buffer1);
}
return base.ProcessCmdKey (ref msg, keyData);
}
}
Note: do not return true in this method, because we want the keydown
message to be processed normally only with shift key being cleared.
This works well on my side. Hope it helps
====================================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
.
- Follow-Ups:
- RE: How to override RichTextBox ProcessCndKey() for text cursor co
- From: Dave Leach
- RE: How to override RichTextBox ProcessCndKey() for text cursor co
- References:
- How to override RichTextBox ProcessCndKey() for text cursor contro
- From: Dave Leach
- How to override RichTextBox ProcessCndKey() for text cursor contro
- Prev by Date: Transparent background component that has mousemove event...
- Next by Date: Re: gdi+ book?
- Previous by thread: How to override RichTextBox ProcessCndKey() for text cursor contro
- Next by thread: RE: How to override RichTextBox ProcessCndKey() for text cursor co
- Index(es):
Relevant Pages
|