0

I'm trying to replace the 'è' character with the 'È' character globally on my computer.

Referring to many answers here on SO Like this I succesfully created a program that replaces for example an 'A' with a 'E' or any other "normal" character. But if I try to pass the È char it doesn't work as expected.

I'm not trying to replace only internally to my app, but globally on every computer click.

This is what I'm doing:

Form1.cs

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Hide();

        KeyInterceptor hook = new KeyInterceptor((int)KeyInterceptor.Modifiers.None, 186, this);
        hook.Register();
    }

    protected override void WndProc(ref Message m)
    {
        Console.WriteLine(m.Msg);
        if (m.Msg == 0x0312)
            HandleHotkey(); // è, which was registered before, was pressed
        base.WndProc(ref m);
    }

    private void HandleHotkey()
    {
        // instead of è send È
        KeyboardManager.PressKey(459007);
    }
}

KeyInterceptor.cs

class KeyInterceptor
{
    [DllImport("user32.dll")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);

    [DllImport("user32.dll")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    public enum Modifiers
    {
        None = 0x0000,
        Alt = 0x0001,
        Control = 0x0002,
        Shift = 0x0004,
        Win = 0x0008
    }

    int modifier;
    int key;
    IntPtr hWnd;
    int id;

    public KeyInterceptor(int modifiers, Keys key, Form f)
    {
        this.modifier = modifiers;
        this.key = (int)key;
        this.hWnd = f.Handle;
        id = this.GetHashCode();
    }
    public KeyInterceptor(int modifiers, char key, Form f)
    {
        this.modifier = modifiers;
        this.key = (int)key;
        this.hWnd = f.Handle;
        id = this.GetHashCode();
    }
    public KeyInterceptor(int modifiers, int key, Form f)
    {
        this.modifier = modifiers;
        this.key = key;
        this.hWnd = f.Handle;
        id = this.GetHashCode();
    }

    public override int GetHashCode()
    {
        return modifier ^ key ^ hWnd.ToInt32();
    }

    public bool Register()
    {
        return RegisterHotKey(hWnd, id, modifier, key);
    }
    public bool Unregister()
    {
        return UnregisterHotKey(hWnd, id);
    }
}

KeyboardManager.cs

public class KeyboardManager
{
    public const int INPUT_KEYBOARD = 1;
    public const int KEYEVENTF_KEYUP = 0x0002;

    public struct KEYDBINPUT
    {
        public Int16 wVk;
        public ushort wScan;
        public Int32 dwFlags;
        public Int32 time;
        public Int32 dwExtraInfo;
        public Int32 __filler1;
        public Int32 __filler2;
    }

    public struct INPUT
    {
        public Int32 type;
        public KEYDBINPUT ki;
    }

    [DllImport("user32.dll")]
    public static extern int SendInput(int cInputs, ref INPUT pInputs, int cbSize);

    public static void HoldKey(Keys vk)
    {
        INPUT input = new INPUT();
        input.type = INPUT_KEYBOARD;
        input.ki.dwFlags = 0;
        input.ki.wVk = (Int16)vk;
        SendInput(1, ref input, Marshal.SizeOf(input));
    }

    public static void ReleaseKey(Keys vk)
    {
        INPUT input = new INPUT();
        input.type = INPUT_KEYBOARD;
        input.ki.dwFlags = KEYEVENTF_KEYUP;
        input.ki.wVk = (Int16)vk;
        SendInput(1, ref input, Marshal.SizeOf(input));
    }
    public static void HoldKey(int vk)
    {
        INPUT input = new INPUT();
        input.type = INPUT_KEYBOARD;
        input.ki.dwFlags = 0;
        input.ki.wVk = (Int16)vk;
        SendInput(1, ref input, Marshal.SizeOf(input));
    }

    public static void ReleaseKey(int vk)
    {
        INPUT input = new INPUT();
        input.type = INPUT_KEYBOARD;
        input.ki.dwFlags = KEYEVENTF_KEYUP;
        input.ki.wVk = (Int16)vk;
        SendInput(1, ref input, Marshal.SizeOf(input));
    }

    public static void PressKey(Keys vk)
    {
        HoldKey(vk);
        ReleaseKey(vk);
    }

    public static void PressKey(int vk)
    {
        HoldKey(vk);
        ReleaseKey(vk);
    }
}

What am I missing? I don't really get how to send that key out.

Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
  • 459007 is a nasty magic number. Write it in hex (0x700FF) to instantly see that HoldKey() can never do anything with it. Your profile says you live in Italy, when I look at an Italian keyboard layout then I only see a way to generate è (key next to P) but no key that produces È, no matter the modifier state (Shift, Ctrl, AltGr). Well, makes this pretty hard to do. – Hans Passant Nov 14 '19 at 10:51
  • @HansPassant oh, so I can only produce characters which are physically placed over my keyboard? that sounds unlucky. My goal was to realiza a way to write the È char (or accented ò, à, ì, ù) without having to type ascii codes. I'll make some futher researches and notify here if i found out a solution. Thanks! – Pier Giorgio Misley Nov 14 '19 at 16:06
  • You asked the wrong question. – Hans Passant Nov 14 '19 at 16:11
  • @HansPassant sorry for that, what should I ask for to make it more understandable? – Pier Giorgio Misley Nov 14 '19 at 16:38

0 Answers0