mrbelk

Is there a way to use the HardwareButton component to emulate a TAB press Unfortunately, I don't see a way to set up that mapping within WM5.

Thanks,

Matthew Belk



Re: .NET Compact Framework Map HardwareButton to TAB button

mrbelk

44 Views and no suggestions Come on now; surely someone's encountered this...

-Matthew





Re: .NET Compact Framework Map HardwareButton to TAB button

Dan Elliott - MSFT

Matthew,

I'm assuming that by "emulate a TAB press" you mean that pressing one of the hardware buttons should activate the next control in the tab order.

The HardwareButton control sends KeyUp and KeyDown events to the active control. To move from one control to the next by pressing the 'Enter' hardware key, add the following event handler to your form:


private void control_KeyUp(object sender, KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{
Control control = sender as Control;
if (null != control)
{
bool result = control.Parent.SelectNextControl(control, true, true, true, true);
}
}

}

Set the KeyUp event for each control on the form to this handler.

A quick note about the SetNextControl method: it sets the next child control of the the control that calls it. To select the next sibling of a control, SelectNextControl must be called by the control's parent using the control as the first parameter. Thanks to Subhag Oak for straightening that out.

HTH
Dan





Re: .NET Compact Framework Map HardwareButton to TAB button

Lars Brange

I tried SelectNextControl() and couldn't get it to work like the ordinary hardware tab-button. I ended up using P/Invoke and keybd_event() to simulate tab key presses. It works perfectly and you only need to set one form property to true and add one form event handler (not one event handler for every control). If you put the code below in a base form and derive all your forms from that form you will get tab-handling automatically - set it and forget it.

1. Add the following P/Invoke code. NOTE: You should wrap this code in a conditional define (for instance DESIGN) which is true when you need VS design support. The VS designer will not let you design your forms if you have P/Invoke code anywhere in your project...


#if !DESIGN
[DllImport("coredll.dll")]
internal extern static void keybd_event(byte bVk, byte bScan, Int32 dwFlags, Int32 dwExtraInfo);
internal const int KEYEVENTF_KEYUP = 0x02;
internal const int VK_TAB = 0x09;
internal const int VK_SHIFT = 0x10;
#endif



2. Set property KeyPreview to true for the form. This gives the form a chance to see the key presses before the controls get them.


3. Add an event handler for the key down event of the form:


private void Form_KeyDown(object sender, KeyEventArgs e)
{
#if !DESIGN
if (e.KeyCode == System.Windows.Forms.Keys.Up)
{
keybd_event(VK_SHIFT, VK_SHIFT, 0, 0);
keybd_event(VK_TAB, VK_TAB, 0, 0);
keybd_event(VK_TAB, VK_TAB, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, VK_SHIFT, KEYEVENTF_KEYUP, 0);
e.Handled = true;
}
else if(e.KeyCode == System.Windows.Forms.Keys.Down)
{
keybd_event(VK_TAB, VK_TAB, 0, 0);
keybd_event(VK_TAB, VK_TAB, KEYEVENTF_KEYUP, 0);
e.Handled = true;
}
#endif
}



Good luck
Lars Brange





Re: .NET Compact Framework Map HardwareButton to TAB button

Dan Elliott - MSFT

Lars,

In what way did SelectNextControl not work as you expected

Thanks
Dan





Re: .NET Compact Framework Map HardwareButton to TAB button

Lars Brange

If I remember correctly I couldn't tab out of a component container (panel). I might have used SelectNextControl() incorrectly though. I used SelectNextControl(GetFocusedControl(), true, true, true, true); where GetFocusedControl() looped over the form controls to find the focused one.

Lars




Re: .NET Compact Framework Map HardwareButton to TAB button

Dan Elliott - MSFT

Lars,

SelectNextControl(GetFocusedControl().Parent, true, true, true, true) should do what you want.

Dan





Re: .NET Compact Framework Map HardwareButton to TAB button

home_of_wang

Hello Lars:

I have encountered to this problem in WM 6, and I tried to use your solution as above.

But it still can't work by myside.

I don't have any idea about it. Could you give some advices

As above solution, the key point is [DllImport("coredll.dll")] or e.Handled = true

Any advice is appreciated.

Thanks!!

Kevin





Re: .NET Compact Framework Map HardwareButton to TAB button

Lars Brange

Hello Kevin,

You must provide more detailed info for me to be able to help you. Do you get to the event handler

Lars




Re: .NET Compact Framework Map HardwareButton to TAB button

home_of_wang

Hello Lars,

Thanks for your reply.

I'm trying to program a keypad driver.

When TAB key pressed, keypad driver scan hardware and get which key pressed.

And then send keybd_event(VK_TAB,VK_TAB,0,0) as key down notification.

keybd_event(VK_TAB,VK_TAB,KEYEVENTF_KEYUP,0) as key up notification.

But now this function can't work when I launch Notes application in WM6.0.

Did you know what I mean

Thanks!!

Kevin





Re: .NET Compact Framework Map HardwareButton to TAB button

home_of_wang

Hello:

I got more test for TAB key.

It can work in Word Mobile by sending "keybd_event(VK_TAB,VK_TAB,0,0)".

But It was fail in Notes application program.

I don't have any idea for it.

Any advice is appreciated.

Thanks!!

Kevin.