如何捕获Ctrl + Shift + N? [英] How can I capture Ctrl+Shift+N?

查看:254
本文介绍了如何捕获Ctrl + Shift + N?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助某些网络情报,我现在可以使用Ctrl + [某些可显示键],Ctrl + Shift + [一些可显示键]和Ctrl + Shift + Alt + [一些可显示键]的组合来添加重音符号变成一个文本框.

With the help of some of the Cyberintelligensia, I am now able to use combinations of Ctrl+[some displayable key], Ctrl+Shift+[some displayable key], and Ctrl+Shift+Alt+[some displayable key] to add accented chars into a textbox.

问题及其答案为

The question and its answers are here.

但是,仍然有一种顽固的组合.我使用了CodeCaster的建议来添加对"Debug.WriteLine(keyData);"的调用.查看被按下的内容(哪些键).在大多数情况下,这可以正常工作;例如,如果我混搭"Ctrl + Shift + E",则会报告:

However, there is still one recalcitrant combination. I used CodeCaster's suggestion to add a call to "Debug.WriteLine(keyData);" to see what in tarnation was being pressed (which keys). This works fine in most cases; for example, if I mash "Ctrl+Shift+E" it reports:

ControlKey, Control
ShiftKey, Shift, Control
E, Shift, Control

因此,它正在按我的期望进行响应,并在文本框中输入É".这项技术帮助我了解了对!"做出响应所需要的内容.字符(D1).

So it is responding as I expect, and enters a "É" in the textbox. That technique helped me to see what was needed to respond to the "!" character (D1).

但是,有一个组合键无法识别,即"Ctrl + Shift + N"(Ctrl + N起作用).当我按"Ctrl + Shift + N"时,输出"窗口将报告:

However, there is one key combination that it is not discerning, namely "Ctrl+Shift+N" (Ctrl+N works). When I press "Ctrl+Shift+N" the Output window reports:

ControlKey, Control
ShiftKey, Shift, Control

IOW,它缺少预期的"N",因此未在文本框中添加任何内容(应添加Ñ").

IOW, it's missing an expected "N", and so nothing is added to the textbox ("Ñ" should be added).

这是唯一失败的情况;所有其他组合键都起作用.

This is the only case that fails; all the other key combinations work.

Ctrl + N 起作用.我在文本框中看到ñ"并得到:

Ctrl+N does work. I see "ñ" in the textbox and get:

ControlKey, Control
N, Control

...在输出"窗口中.

...in the Output window.

为什么Ctrl + Shift + N和弦中的"N"不是听到"的,我该如何纠正?

Why is the "N" in the Ctrl+Shift+N chord not "heard" and how can I rectify this?

这是我现在拥有的代码:

This is the code that I have now:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    Debug.WriteLine(keyData);
    if (this.ActiveControl != null && this.ActiveControl is TextBox)
    {
        string replacement = "";
        TextBox tb = (TextBox)this.ActiveControl;
        bool useHTMLCodes = checkBoxUseHTMLCodes.Checked;

        // ...

        // N
        if (keyData == (Keys.Control | Keys.N))
        {
            replacement = useHTMLCodes ? "ñ" : "ñ";
        }
        else if (keyData == (Keys.Control | Keys.Shift | Keys.N))
        {
            replacement = useHTMLCodes ? "Ñ" : "Ñ"; // not working
        }

        // ...

        if (replacement != "")
        {
            tb.SelectedText = replacement;
            return true;
        }
    }

    return base.ProcessCmdKey(ref msg, keyData);
}

如前所述,除了捕获Ctrl + Shift + N的代码(应该发出Ñ")的所有内容之外,其他所有东西都起作用.

As mentioned, everything works except the code to trap Ctrl+Shift+N, which should emit "Ñ".

我添加了这个:

tb.ShortcutsEnabled = false;

...来自此处,但这无济于事.

...from here but it doesn't help matters.

奇怪的是,打开记事本的Ctrl + Shift + N键盘快捷键在我的台式机上不再起作用(我正在使用正在讨论的实用程序),但是它在上确实起作用远程桌面会话(我在其中处理Sharepoint的东西).

An odd thing is that the Ctrl+Shift+N keyboard shortcut to open Notepad no longer works on my desktop (where I am working with the utility under discussion), but it does work in a remote desktop session (where I work on Sharepoint stuff).

远程桌面连接真的拦截了桌面上的击键吗?

Is the remote desktop connection really intercepting keystrokes on the desktop?

即使从远程桌面会话注销,盲点"(Ctrl + Shift + N)仍保留在此桌面实用程序内.

Even when logged off from the remote desktop session, though, the "blind spot" (Ctrl+Shift+N) remains within this desktop utility.

推荐答案

请尝试一下.它会告诉我们是否有人已经通过RegisterHotkey()将Ctrl-Shift-N注册为热键组合:

Try this please. It'll tell us if someone else has already registered Ctrl-Shift-N as a hotkey combo via RegisterHotkey():

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private const int MOD_ALT = 0x0001;
    private const int MOD_CONTROL = 0x0002;
    private const int MOD_SHIFT = 0x0004;
    private const int MOD_WIN = 0x0008;

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    private void button1_Click(object sender, EventArgs e)
    {
        bool result = RegisterHotKey(this.Handle, 1001, MOD_CONTROL | MOD_SHIFT, (int)Keys.N);
        if (result)
        {
            UnregisterHotKey(this.Handle, 1001);
        }

        string msg = result ? " was NOT " : " WAS ";
        MessageBox.Show("The Ctrl-Shift-N combination" + msg + "already registered on your system.");
    }

}

如果再次出现该错误,则说明该组合已经在您的系统上注册,那么它将无法到达您的应用程序,因为该组合已经被注册该组合的应用消耗"了.

If this comes back saying that combo was already registered on your system, then it will not reach your app as the combo will already be "consumed" by the app that registered the combo.

如果该组合没有注册,则可能是另一个应用程序正在通过低级键盘挂钩(WH_KEYBOARD_LL)捕获它并从那里使用它.但是,您将无从得知……

If that combo is NOT registered, then it's possible that another application is trapping it via a low level keyboard hook (WH_KEYBOARD_LL) and consuming it from there. You'd have no way of knowing this, however...

这篇关于如何捕获Ctrl + Shift + N?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆