How can I capture Ctrl+Shift+N?

独自空忆成欢 提交于 2020-01-14 02:51:27

问题


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.

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

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).

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, 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 does work. I see "ñ" in the textbox and get:

ControlKey, Control
N, Control

...in the Output window.

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);
}

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

UPDATE

I added this:

tb.ShortcutsEnabled = false;

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

UPDATE 2

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?

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


回答1:


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.

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...



来源:https://stackoverflow.com/questions/31188543/how-can-i-capture-ctrlshiftn

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