RE: How to override RichTextBox ProcessCndKey() for text cursor contro



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.

.



Relevant Pages

  • RE: How to override RichTextBox ProcessCndKey() for text cursor co
    ... TAB versus Shift-Tab with my previous question? ... > Hi Dave, ... > such as TAB, RETURN, ESCAPE, and arrow keys(for keyboard navigation). ... > in window procedure of RichEdit control, it will determine if the shift key ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Unexplained shutdown please help
    ... Open the Control Panel, Regional & Languages options, Languages tab ... is your keyboard and that it is the correct language version. ... Hi Terry - I'm using a dell pc, ...
    (microsoft.public.word.application.errors)
  • Re: Cloud Date Entry
    ... When I was first using windows I wondered why it always wanted a <Tab> ... the typewriter even though it was a multi-line form (she did have to ... I stated that typists use both hands on the keyboard, ... QWERTY or AZERTY used by typists. ...
    (comp.databases.pick)
  • Re: AKICIF (new PC)
    ... I gave it to her because the keyboard is poorly designed--an up arrow ... key next to the small right shift key. ... I later bought myself an Acer Aspire, a similar netbook with a much ... so I end up using my Mac laptop instead. ...
    (rec.arts.sf.fandom)
  • Key event and focus handling
    ... manage ALL keyboard events for the component and all components within ... the current selected tab) should give the component as a whole focus. ... focus or key event handling for the main tab pane component. ... Register a keyboard action for every possible keystroke? ...
    (comp.lang.java.gui)

Quantcast