1
votes

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
Is this in your top level window? - rene
Works for me, prints N, Shift, Control. So something is processing the key combination before it ends up in your form. Do you have a menu or other controls on this form, what is your keyboard layout, do you have a hotkey program, ... - CodeCaster
@rene: I'm not sure what you mean; this is the only code on the form (except the "InitializeComponent();" in the form's constructor). - B. Clay Shannon
@CodeCaster: No menus on the form; keyboard layout is standard/nothing fancy. No hotkey program that I know of (nothing I installed, that is to say). Come to think of it, though, I did have Ctrl+Shift+N set up to open Notepad, yet that isn't even working anymore. Is there a way to temporarily ignore such while this app is running? - B. Clay Shannon
That would be amazing. I just asked becasue VS might have interferd.. - TaW

1 Answers

4
votes

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