Consuming the starpad Return button?

F

fd123456

Hi all!

In a Form's constructor, I add a KeyEvent handler :

this.KeyDown += new KeyEventHandler(myKeyDown);

And here is myKeyDown :

private void myKeyDown(object sender, KeyEventArgs e)
{
e.Handled=true;
MessageBox.Show(e.KeyCode.ToString());
}

The problem is that if the key pressed is the starpad's middle button
(which, weirdly, is caught as Select AND Return AND F23 by CF), the
event is not consumed, e is not handled, and the MessageBox is closed
because it thinks I'm still pressing Return.

This is a very basic repro, but on a larger scale, it means that you
can not have two successive forms both having the Return key as a
validation, because as soon as the first one calls the second one, the
second one gets validated as well.

Is there a way to consume that OK/Select/Return/F23/middle key event?
I've looked all around and couldn't find anything. Having a loop
waiting for the KeyUp event is not possible, because that event
doesn't get consumed either. And waiting for SP2/3/Whidbey/VS 2018 is
not an option :). Has anyone found a workaround?

Thanks for sharing if you have.

Michel.
 
F

fd123456

Ok, found a totally inelegant solution...

After InitializeComponent() :

this.KeyDown+=new KeyEventHandler(MyKeyDown);
this.KeyUp+=new KeyEventHandler(MyKeyUp);

In declarations :

private bool IsUp=false;

Anywhere :

private void MyKeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
IsUp=false;
e.Handled=true;
while (IsUp==false) Application.DoEvents();
// do stuff here, eg MessageBox.Show(e.KeyCode.ToString());
}
private void MyKeyUp(object sender, System.Windows.Forms.KeyEventArgs
e)
{
IsUp=true;
}

Drawback : the system waits until you've released the key to carry on
with the rest of the code. I just wish the Handled method worked as
expected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top