How to turn off all touch input at application, wi

2020-02-10 10:06发布

问题:

Using c# for a wpf application, if in Windows 7 touch is enabled in the control panel, a user by default can 'write' on an InkCanvas with a finger. I want to disable that and force stylus input only.

I'd like to know how to do it more than one way if possible: first by disabling touch on the InkCanvas, second by disabling it for a particular window, and third by disabling it for the entire application. A bonus fourth would be knowing how to turn touch on or off system-wide.

I have tried UnregisterTouchWindow, and I have tried setting Stylus.SetIsTouchFeedbackEnabled to false for the InkCanvas, but neither has worked.

回答1:

Further digging helped me put together the following as a way to toggle touch on/off system-wide. If anyone knows how to accomplish this in any of the other 3 ways, I'd still appreciate those answers.

The basic steps are to check the current registry status, change it if necessary (and then refresh the system to recognize the change), and make note of the initial state to restore if needed on program exit.

Thanks to the posters at these two links for the education.

public MainWindow(){

    InitializeComponent();

    RegistryKey regKey = Registry.CurrentUser;
    regKey = regKey.OpenSubKey(@"Software\Microsoft\Wisp\Touch", true);
    string currKey = regKey.GetValue("TouchGate").ToString();
    if (currKey == "1")
    {
        regKey.SetValue("TouchGate", 0x00000000);
        User32Utils.Notify_SettingChange();
        UserConfig.TGate = "1";
    }
    regKey.Close();
    ...
}


public static class UserConfig {
    public static string TGate { get; set; }
    ...
}


private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e){
    ...
    if (UserConfig.TGate == "1")
    {
        RegistryKey regKey = Registry.CurrentUser;
        regKey = regKey.OpenSubKey(@"Software\Microsoft\Wisp\Touch", true);
        regKey.SetValue("TouchGate", 0x00000001);
        User32Utils.Notify_SettingChange();
        regKey.Close();
    }
}



//------------------User32Utils.cs
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace (...)
{
    internal class User32Utils
    {
        #region USER32 Options
        static IntPtr HWND_BROADCAST = new IntPtr(0xffffL);
        static IntPtr WM_SETTINGCHANGE = new IntPtr(0x1a);
        #endregion

        #region STRUCT
        enum SendMessageTimeoutFlags : uint
        {
            SMTO_NORMAL = 0x0000,
            SMTO_BLOCK = 0x0001,
            SMTO_ABORTIFHUNG = 0x2,
            SMTO_NOTIMEOUTIFNOTHUNG = 0x0008
        }
        #endregion

        #region Interop

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern IntPtr SendMessageTimeout(IntPtr hWnd,
                                                uint Msg,
                                                UIntPtr wParam,
                                                UIntPtr lParam,
                                                SendMessageTimeoutFlags fuFlags,
                                                uint uTimeout,
                                                out UIntPtr lpdwResult);
        #endregion

        internal static void Notify_SettingChange()
        {
            UIntPtr result;
            SendMessageTimeout(HWND_BROADCAST, (uint)WM_SETTINGCHANGE,
                               UIntPtr.Zero, UIntPtr.Zero,
                                SendMessageTimeoutFlags.SMTO_NORMAL, 1000, out result);
        }
    }
}