Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

cefsharp not passing keydown to form

public class KeyboardHandler : IKeyboardHandler
{

    public bool OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
    {
        bool result = false;
        Debug.WriteLine(String.Format("OnKeyEvent: KeyType: {0} 0x{1:X} Modifiers: {2}", type, windowsKeyCode, modifiers));
        // TODO: Handle MessageNeeded cases here somehow.
        return result;
    }

    public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)
    {
        const int WM_SYSKEYDOWN = 0x104;
        const int WM_KEYDOWN = 0x100;
        const int WM_KEYUP = 0x101;
        const int WM_SYSKEYUP = 0x105;
        const int WM_CHAR = 0x102;
        const int WM_SYSCHAR = 0x106;
        const int VK_TAB = 0x9;

        bool result = false;

        isKeyboardShortcut = false;

        // Don't deal with TABs by default:
        // TODO: Are there any additional ones we need to be careful of?
        // i.e. Escape, Return, etc...?
        if (windowsKeyCode == VK_TAB)
        {
            return result;
        }

        Control control = browserControl as Control;
        int msgType = 0;
        switch (type)
        {
            case KeyType.RawKeyDown:
                if (isSystemKey)
                {
                    msgType = WM_SYSKEYDOWN;
                }
                else
                {
                    msgType = WM_KEYDOWN;
                }
                break;
            case KeyType.KeyUp:
                if (isSystemKey)
                {
                    msgType = WM_SYSKEYUP;
                }
                else
                {
                    msgType = WM_KEYUP;
                }
                break;
            case KeyType.Char:
                if (isSystemKey)
                {
                    msgType = WM_SYSCHAR;
                }
                else
                {
                    msgType = WM_CHAR;
                }
                break;
            default:
                Trace.Assert(false);
                break;
        }
        // We have to adapt from CEF's UI thread message loop to our fronting WinForm control here.
        // So, we have to make some calls that Application.Run usually ends up handling for us:
        PreProcessControlState state = PreProcessControlState.MessageNotNeeded;
        // We can't use BeginInvoke here, because we need the results for the return value
        // and isKeyboardShortcut. In theory this shouldn't deadlock, because
        // atm this is the only synchronous operation between the two threads.
        control.Invoke(new Action(() =>
        {
            Message msg = new Message() { HWnd = control.Handle, Msg = msgType, WParam = new IntPtr(windowsKeyCode), LParam = new IntPtr(nativeKeyCode) };

            // First comes Application.AddMessageFilter related processing:
            // 99.9% of the time in WinForms this doesn't do anything interesting.
            bool processed = Application.FilterMessage(ref msg);
            if (processed)
            {
                state = PreProcessControlState.MessageProcessed;
            }
            else
            {
                // Next we see if our control (or one of its parents)
                // wants first crack at the message via several possible Control methods.
                // This includes things like Mnemonics/Accelerators/Menu Shortcuts/etc...
                state = control.PreProcessControlMessage(ref msg);
            }
        }));
        if (state == PreProcessControlState.MessageNeeded)
        {
            // TODO: Determine how to track MessageNeeded for OnKeyEvent.
            isKeyboardShortcut = true;
        }
        else if (state == PreProcessControlState.MessageProcessed)
        {
            // Most of the interesting cases get processed by PreProcessControlMessage.
            result = true;
        }
        Debug.WriteLine(String.Format("OnPreKeyEvent: KeyType: {0} 0x{1:X} Modifiers: {2}", type, windowsKeyCode, modifiers));
        Debug.WriteLine(String.Format("OnPreKeyEvent PreProcessControlState: {0}", state));
        return result;
    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: large blank file C# 
Csharp :: c# remove exit icon 
Csharp :: google sheets problems cell not considered even 
Csharp :: how to change the volume of all sound effects in monogame 
Csharp :: c# read single key 
Csharp :: telerik mvc grid scroll 
Csharp :: list of countries in .net mvc 5 
Csharp :: c# use readonly array as method default 
Csharp :: download xml file asp.net web api 
Csharp :: c# delegates 
Csharp :: C# oledb excel select column with space 
Csharp :: asp.net core relative file path within console app 
Csharp :: c# fileinfo filename without extension 
Csharp :: C# Zip large files causes OOM exception 
Csharp :: C# enum fglag 
Csharp :: add new page itext 7 
Csharp :: string to float c# 
Csharp :: c# .net set exception data 
Csharp :: how to execute a function in every object of list c# 
Csharp :: Accepts one of 1, 2, x or X, or nothing 
Csharp :: Garbage collect every 30 frames unity 
Csharp :: range to 01 
Csharp :: c# get app FileVersion 
Csharp :: math round to next integer c# 
Csharp :: c# properties making string required 
Csharp :: ExpandoObject Syntax that Compile 
Csharp :: c# check file similarities 
Csharp :: unity save slots 
Csharp :: spring jar debug level running 
Csharp :: viewresolver cyrillic 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =