如何判断哪个物理设备点击了C#中的按钮?

Jud*_*ira 5 c# mouse touchscreen click button

我有一个带按钮的表格.此应用程序旨在在触摸屏计算机上运行.单击按钮时,我想知道它是否被鼠标或触摸屏点击.

这可能吗?如果是这样,怎么样?

Wil*_*orn 3

private void button_Click(object sender, EventArgs e)
{
    try
    {
        ((MouseEventArgs)e).Button.ToString();
    }
    catch(Exception)
    {
        //If an exception is catch, it means the mouse was not used.
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一种粗俗的方法,因为只要按钮被鼠标以外的其他东西“单击”,无论是触摸屏还是键盘的返回,它都会捕获异常。但它会完成这项工作:)