Skip to content

Commit 29e0167

Browse files
committed
Update KeyboardHandler to ignore left, right, up, down as they were being cancelled
Some resharper inspired cleanup as well
1 parent 63ff016 commit 29e0167

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

CefSharp.WinForms.Example/Handlers/KeyboardHandler.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,25 @@ public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType
1616
const int WM_CHAR = 0x102;
1717
const int WM_SYSCHAR = 0x106;
1818
const int VK_TAB = 0x9;
19-
20-
bool result = false;
19+
const int VK_LEFT = 0x25;
20+
const int VK_UP = 0x26;
21+
const int VK_RIGHT = 0x27;
22+
const int VK_DOWN = 0x28;
2123

2224
isKeyboardShortcut = false;
2325

2426
// Don't deal with TABs by default:
2527
// TODO: Are there any additional ones we need to be careful of?
2628
// i.e. Escape, Return, etc...?
27-
if (windowsKeyCode == VK_TAB)
29+
if (windowsKeyCode == VK_TAB || windowsKeyCode == VK_LEFT || windowsKeyCode == VK_UP || windowsKeyCode == VK_DOWN || windowsKeyCode == VK_RIGHT)
2830
{
29-
return result;
31+
return false;
3032
}
3133

32-
Control control = browserControl as Control;
33-
int msgType = 0;
34+
var result = false;
35+
36+
var control = browserControl as Control;
37+
var msgType = 0;
3438
switch (type)
3539
{
3640
case KeyType.RawKeyDown:
@@ -69,17 +73,23 @@ public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType
6973
}
7074
// We have to adapt from CEF's UI thread message loop to our fronting WinForm control here.
7175
// So, we have to make some calls that Application.Run usually ends up handling for us:
72-
PreProcessControlState state = PreProcessControlState.MessageNotNeeded;
76+
var state = PreProcessControlState.MessageNotNeeded;
7377
// We can't use BeginInvoke here, because we need the results for the return value
7478
// and isKeyboardShortcut. In theory this shouldn't deadlock, because
7579
// atm this is the only synchronous operation between the two threads.
7680
control.Invoke(new Action(() =>
7781
{
78-
Message msg = new Message() { HWnd = control.Handle, Msg = msgType, WParam = new IntPtr(windowsKeyCode), LParam = new IntPtr(nativeKeyCode) };
82+
var msg = new Message
83+
{
84+
HWnd = control.Handle,
85+
Msg = msgType,
86+
WParam = new IntPtr(windowsKeyCode),
87+
LParam = new IntPtr(nativeKeyCode)
88+
};
7989

8090
// First comes Application.AddMessageFilter related processing:
8191
// 99.9% of the time in WinForms this doesn't do anything interesting.
82-
bool processed = Application.FilterMessage(ref msg);
92+
var processed = Application.FilterMessage(ref msg);
8393
if (processed)
8494
{
8595
state = PreProcessControlState.MessageProcessed;
@@ -92,6 +102,7 @@ public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType
92102
state = control.PreProcessControlMessage(ref msg);
93103
}
94104
}));
105+
95106
if (state == PreProcessControlState.MessageNeeded)
96107
{
97108
// TODO: Determine how to track MessageNeeded for OnKeyEvent.
@@ -102,16 +113,18 @@ public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType
102113
// Most of the interesting cases get processed by PreProcessControlMessage.
103114
result = true;
104115
}
105-
Debug.WriteLine(String.Format("OnPreKeyEvent: KeyType: {0} 0x{1:X} Modifiers: {2}", type, windowsKeyCode, modifiers));
106-
Debug.WriteLine(String.Format("OnPreKeyEvent PreProcessControlState: {0}", state));
116+
117+
Debug.WriteLine("OnPreKeyEvent: KeyType: {0} 0x{1:X} Modifiers: {2}", type, windowsKeyCode, modifiers);
118+
Debug.WriteLine("OnPreKeyEvent PreProcessControlState: {0}", state);
119+
107120
return result;
108121
}
109122

110123
/// <inheritdoc/>>
111124
public bool OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
112125
{
113-
bool result = false;
114-
Debug.WriteLine(String.Format("OnKeyEvent: KeyType: {0} 0x{1:X} Modifiers: {2}", type, windowsKeyCode, modifiers));
126+
var result = false;
127+
Debug.WriteLine("OnKeyEvent: KeyType: {0} 0x{1:X} Modifiers: {2}", type, windowsKeyCode, modifiers);
115128
// TODO: Handle MessageNeeded cases here somehow.
116129
return result;
117130
}

0 commit comments

Comments
 (0)