How to control lifetime of key modifiers like SHIFT?
- From: "Dave Leach" <babel@xxxxxxxxxxxxxxxx>
- Date: Wed, 6 Jul 2005 11:36:14 -0700
I am writing a WindowsForms application using VS.NET in C#. My application
will run in an environment that includes a USB keyboard-type of user input
device. The USB user input device sends normal keycodes for data entry and
navigating the forms and controls presented by the application. For example,
the device will send keycodes that correspond to most of the Keys enumeration
members, like A, B, D0, D1, Up, Down, Enter, etc. The standard modifiers
like Shift and Control are used as well.
The USB input device provides various buttons and a rotary input device
(knob). Two of the buttons are intended to operate just like the up and down
arrows of a standard keyboard, generating the Keys Up and Down keycodes. The
knob device is intended to provide similar behavior to the up and down arrow
keys, but the application must be able to distinguish between the up and down
keys and the knob, so the knob sends the Up and Down keycodes with the Shift
modifier applied.
My application uses controls that are derived from the standard controls
like TextBox and ComboBox. The derived versions of the controls implement
the ProcessCmdKey() method so that I can provide specialized behaviors, like
creating a numeric entry control from the standard TextBox control.
The problem I have run into relates to the lifetime of the key modifier
Shift. When a derived control receives the Up or Down keycodes, it may want
to have focus navigate away from it to the next control in the tabbing order.
To do this I use the SendKeys.SendWait() method, sending either the Tab or
Shift-Tab (actually "{TAB}" or "+{TAB}"). However, if the control receives
the Shift modified Up or Down keycodes (from the knob), sending Tab or
Shift-Tab always acts like Shift-Tab was sent.
Apparently, the Shift modifier that was applied to the received Up or Down
arrow is also applied to the Tab key being sent using the SendKeys.SendWait()
call.
How can I remove the modifier state prior to sending the Tab key?
Here is a code snippet from a derived control. See the NOTE comment in the
code.
protected override bool ProcessCmdKey( ref Message msg, Keys key )
{
if ( this.active )
{
if ( key == Keys.Down || key == ( Keys.Shift | Keys.Up ) )
{
DecrementCurrentValue();
}
else if ( key == Keys.Up || key == ( Keys.Shift | Keys.Down ) )
{
IncrementCurrentValue();
}
}
else
{
if ( key == Keys.Down || key == ( Keys.Shift | Keys.Down ) )
{
SendKeys.SendWait( "{TAB}" ); // NOTE: Shift-Down produces Shift-Tab!!!
}
else if ( key == Keys.Up || key == ( Keys.Shift | Keys.Up ) )
{
SendKeys.SendWait( "+{TAB}" );
}
}
return true;
}
Thanks,
Dave
.
- Follow-Ups:
- RE: How to control lifetime of key modifiers like SHIFT?
- From: "Jeffrey Tan[MSFT]"
- RE: How to control lifetime of key modifiers like SHIFT?
- Prev by Date: Validation detection in custom controls...
- Next by Date: Binding DataGridView to Generic SortedList
- Previous by thread: Validation detection in custom controls...
- Next by thread: RE: How to control lifetime of key modifiers like SHIFT?
- Index(es):