我可以取消在控制台中选择文字吗?

时间:2016-07-28 08:41:37

标签: c#

我正在用C#制作一个控制台应用程序,我想知道是否可以在按住shift和按箭头时禁用选择文本。

我尝试使用Console.CancelKeyPress,但在这种情况下它不起作用。

1 个答案:

答案 0 :(得分:0)

尝试使用这些方法:

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
    LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);

而不是钩子方法:

private static IntPtr HookCb(int nCode, IntPtr wParam, IntPtr lParam)
{
    if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
    {
        int keyCode = Marshal.ReadInt32(lParam);        
        string keyName = ((Keys)keyCode).ToString();  
        //there you can define which  key was pressed and react accordingly
    }}