Simulate Keyboard Event on Windows Mobile

ぃ、小莉子 提交于 2020-01-01 16:58:31

问题


Please refer to the screenshot below:

The datetime control is a Compact Framework DateTimePicker, the numbered buttons are stndard Button controls.

Clicking on the arrow of the DateTimePicker displays a calendar control allowing the suer to select a date. However if the user clicks on any part of the selected Text date it is highlighted and can be amended using either the hard keys or the on-screen keyboard. I'm trying to emulate this functionality with my standard buttons.

I've have tried the following but currently this is only causing the DateTimePicker to lose focus.

    const int KEYEVENTF_KEYUP = 0x2;
    const int KEYEVENTF_KEYDOWN = 0x0;
    const int VK_NUMPAD1 = 0x61;

    private void digitButton_Clicked(object sender, EventArgs e)
    {
        keybd_event(VK_NUMPAD1, 0, KEYEVENTF_KEYDOWN, 0);
        keybd_event(VK_NUMPAD1, 0, KEYEVENTF_KEYUP, 0);
    }

    [DllImport("coredll.dll", EntryPoint = "keybd_event", SetLastError = true)]
    public static extern void keybd_event
    (
    byte bVk,
        byte bScan,
    int dwFlags,
        int dwExtraInfo
    ); 


回答1:


I think you pointed out your problem, "...but currently this is only causing the DateTimePicker to lose focus". I suspect your buttons are gaining focus and as a result are consuming the keystrokes. You might try setting focus to the DateTimePicker before generating the keystrokes. If that fails, as a last resort you can hard code to the control using SendMessage() to the window handle for the DateTimePicker, with WM_KEYDOWN, WM_KEYUP.




回答2:


Try the following - while I haven't tested it for your exact scenario, it does work in our .NET CF applications:

const int KEYEVENTF_EXTENDED_KEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
const int KEYEVENTF_KEYDOWN = 0x0;
const int VK_NUMPAD1 = 0x61;

private void digitButton_Clicked(object sender, EventArgs e)
{
    keybd_event(VK_NUMPAD1, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
    keybd_event(VK_NUMPAD1, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}

[DllImport("coredll.dll", EntryPoint = "keybd_event", SetLastError = true)]
public static extern void keybd_event
(
byte bVk,
    byte bScan,
int dwFlags,
    int dwExtraInfo
); 



回答3:


keybd_event works fine, BUT you have to focus the datePicker first:

(on a form with one datePicker and 12 buttons (button1 to button12)):

    public Form1()
    {
        InitializeComponent();
        foreach (Control c in this.Controls)
        {
            if (c is Button)
            {
                int n = getButtonNumber((Button)c);
                if (n <= 10)
                    c.Text = n.ToString();
                c.Click += new EventHandler(c_Click);
            }
        }
        button11.Text = "<-"; button12.Text = "OK";
    }
    void c_Click(object sender, EventArgs e)
    {
        if (sender is Button)
        {
            Button b = (Button)sender;
            int n = Convert.ToInt16( b.Text);
            digitButtonClick(n);
        }
    }
    private void digitButtonClick(int n)
    {
        byte vkInt = (byte)(0x30 + n);
        dateTimePicker1.Focus();
        keybd_event(vkInt, 0, KEYEVENTF_KEYDOWN, 0);
        keybd_event(vkInt, 0, KEYEVENTF_KEYUP, 0);
    }

regards

Josef



来源:https://stackoverflow.com/questions/13014798/simulate-keyboard-event-on-windows-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!