0

I'm clicking on an inputfield to login into my account. The problem is that right now I'm getting all letters uppercase. In the screenshot you can see the inputfield were I typed "aaaa" but I get "AAAA". enter image description here

1.Here ppl are saying that the KeyEvent cant handle lowercase, but I dont know what alternative I have then. The strange part is, that typing on the keypad creates lowercase letters. E.g. pressing the "7" on keypad will give me a "g". And using string or char is not recommended, becaus at the end I need a integer/key.

2. This are right now my lines:

        private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (!(Tag is TcpDeviceClient client)) return;
        if (e.Key == Key.Back) e.Handled = true;
        client?.SetKey(KeyInterop.VirtualKeyFromKey(e.Key), Keyboard.Modifiers.ToChromiumMods());

I inserted a Console.WriteLine that u can see in the screenshot (here its missing cause I wanted to save space). Every time Im typing I get three different formats of the key and these don't match my input. Please note: In the console it shows different letters. The letters showed in the inputfield are right(but in uppercase).

My issue How can I get the right pressed key in lowercase?

Perazim
  • 1,501
  • 3
  • 19
  • 42
  • In the link you posted, some of the solutions involve handling `TextInput` or `PreviewTextInput`, did you try any of those? – redcurry Feb 12 '19 at 14:56
  • I tried TextInput halfway, but these means that I have to use String and at the end cast back to Key. I guess the cast will fail. Right now Im using e.Key, lets say the value is 85 and stands for B. If I will youe TextInput I will get a string "B" for B and this I cant cast to Key so it gets to 85, right? – Perazim Feb 12 '19 at 14:59
  • I guess it depends on what you want to do with the characters that are entered. Do you need the entire text, or just each of the characters that were pressed? Must you have a `Key` type or could it be a `char`? – redcurry Feb 12 '19 at 15:02
  • I would like to stay with `Key` and right now its just each character that I pass. I guess there is some issue in keylayout/System.Windows.Input or not? – Perazim Feb 12 '19 at 15:03
  • 1
    If you get "B" (a `string`), you can get the character 'B' (a `char`): `myString[0]`. To get the ASCII value, you can just cast the character to an `int`: `(int)myString[0]`. – redcurry Feb 12 '19 at 15:09
  • Ok, Im gonna try this tomorrow. Ty! – Perazim Feb 12 '19 at 15:15
  • However, I just found out that you won't be able to detect any spaces using `PreviewTextInput`: https://stackoverflow.com/q/7971559/1383366. – redcurry Feb 12 '19 at 15:18
  • How does CefSharp fit into your question exactly? It's in the title only from what I can tell – amaitland Feb 13 '19 at 01:19
  • @redcurry It worked!:) I converted the key to unicode and casted it then to char. I'm gonna poste my code below. – Perazim Feb 14 '19 at 10:00
  • @amaitland Im using cefsharp on my server to get an image/screenshot of my off-screen-browser. The client (you can see his view in the screenshot above) can interact with the image. Thats why I intercept mouse click and which keys are pressed to send it afterwards to cef. – Perazim Feb 14 '19 at 10:00

1 Answers1

0

I post the answer for coming visitors with the same problem. I have to mention that this is not the perfect answer/solution:

So I'm still using KeyEventArgs, taking the key and sending it to the function KeyCodeToUnicode(). But before that u have to convert the WPF Key into a Win32 Virtual-Key.

    private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (!(Tag is TcpDeviceClient client)) return;

        Char str = KeyCodeToUnicode((Key)KeyInterop.VirtualKeyFromKey(e.Key));
        Console.WriteLine("(int)e.Key: " + (int)e.Key + "\n VirtualKeyFromKey(e.Key): " + KeyInterop.VirtualKeyFromKey(e.Key) + "\n (Key)VirtualKeyFromKey(e.Key): " + (Key)KeyInterop.VirtualKeyFromKey(e.Key) + "\n KeyCodeToUnicode(e.Key)->str: " + str);
        Console.ReadLine();

        client?.SetKey(str, Keyboard.Modifiers.ToChromiumMods());
    }

And here I'm getting an char back.

    public Char KeyCodeToUnicode(Key key)
    {
        byte[] keyboardState = new byte[255];
        bool keyboardStateStatus = GetKeyboardState(keyboardState);

        if (!keyboardStateStatus)
        {
            return '\0';
        }

        uint virtualKeyCode = (uint)key;
        uint scanCode = (uint)MapVirtualKey(virtualKeyCode, 0);
        IntPtr inputLocaleIdentifier = GetKeyboardLayout(0);

        StringBuilder result = new StringBuilder();
        ToUnicodeEx(virtualKeyCode, scanCode, keyboardState, result, (int)5, (uint)0, inputLocaleIdentifier);

        if(result.ToString().Length < 1)
            return '\0';
        else
            return Convert.ToChar(result.ToString());
    }

And in my InputKeyCode() the key gets implicit casted to int again.

    public void InputKeyCode(Char keycode, int modifier)
    {
        var host = Browser.GetBrowser().GetHost();

        KeyEvent kv = new KeyEvent();
        kv.WindowsKeyCode = keycode;
        //....
Perazim
  • 1,501
  • 3
  • 19
  • 42
  • 1
    This will be horribly slow, see https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf/Internals/WpfKeyboardHandler.cs for how the WPF version does it. – amaitland Feb 14 '19 at 10:31
  • Hey, do you have an update on this? I was trying to do basically the same thing you explained but I can't seem to correctly assign WindowsKeyCode and NativeKeyCode, the behaviour seems to be correct, meaning I can see the characters I type on google search box example, but if I use a debugger to check keyboard DOM I'm getting funny results https://dvcs.w3.org/hg/d4e/raw-file/tip/key-event-test.html – AzeExMachina Apr 02 '21 at 15:30
  • I can check on Tuesday because it was a long time ago, but I think it has stayed as it says here. – Perazim Apr 03 '21 at 19:36