简体   繁体   中英

SendKeys with C# PostMessage - Underscore

I'm trying to send an underscore with PostMessage but the best I can get is -, can anyone help here I cannot find the answer anywhere.

I send a string to a character loop that gets each one and uses PostMessage to send the key, which works fine for almost everything but I cannot figure out _.

public bool SendKeys(string message)
    {
        var success = false;
        uint wparam = 0 << 29 | 0;
        foreach (var child in GetChildWindows(Window.Windowhandle))
        {
            var sb = new StringBuilder(100);
            GetClassName(child, sb, sb.Capacity);
            //(IntPtr)Keys.H
            if (sb.ToString() != Window.TargetWindow) continue;
            foreach (var c in message)
            {
                var k = ConvertFromString(c.ToString());
                if (String.Equals(c.ToString(), c.ToString().ToUpper(), StringComparison.Ordinal))
                {
                    PostMessage(child, WM_CHAR, (IntPtr) k, (IntPtr) wparam);
                }
                else
                {
                    PostMessage(child, WM_KEYDOWN, (IntPtr)k, (IntPtr)wparam);
                }
                success = true;
                Thread.Sleep(200);
            }
        }
        return success;
    }

ConvertFromString is just (Keys)Enum.Parse(typeof (Keys), keystr); really, getting the key from System.Form.Keys

Can anyone identify how to send an underscore??

ConvertFromString is just (Keys)Enum.Parse(typeof (Keys), keystr);

That is the fundamental flaw, there is no Keys enumeration value that represent an underscore. Keys are virtual keys , they are the same anywhere in the world. But the characters they produce are not the same, it depends the modifier keys and the active keyboard layout that the user selected.

An American user must first press the Shift key, then press Keys.OemMinus. A German user must press Shift, then Keys.OemQuestion. A French user only presses Keys.D8. A Spanish user presses AltGr, then Keys.D7. Etcetera.

You'll need to get ahead by dropping your dependency on Keys if you want a predictable result. Use WM_CHAR and pass the actual character you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM