|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Data; |
| 5 | +using System.Drawing; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Windows.Forms; |
| 10 | +using CefSharp; |
| 11 | +using CefSharp.WinForms; |
| 12 | + |
| 13 | +namespace Jstris_Client |
| 14 | +{ |
| 15 | + public partial class Jstris : Form |
| 16 | + { |
| 17 | + public ChromiumWebBrowser browser; |
| 18 | + public Jstris() |
| 19 | + { |
| 20 | + InitializeComponent(); |
| 21 | + this.FormBorderStyle = FormBorderStyle.Sizable; |
| 22 | + this.WindowState = FormWindowState.Maximized; |
| 23 | + InitializeChromium(); |
| 24 | + |
| 25 | + } |
| 26 | + |
| 27 | + public void InitializeChromium() |
| 28 | + { |
| 29 | + CefSettings settings = new CefSettings(); |
| 30 | + settings.CachePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\CEF"; |
| 31 | + Cef.Initialize(settings); |
| 32 | + browser = new ChromiumWebBrowser("https://jstris.jezevec10.com"); |
| 33 | + browser.KeyboardHandler = new KeyboardHandler(); |
| 34 | + this.Controls.Add(browser); |
| 35 | + browser.Dock = DockStyle.Fill; |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + private void Jstris_KeyDown(object sender, KeyEventArgs e) |
| 40 | + { |
| 41 | + if (e.KeyCode == Keys.Escape) |
| 42 | + { |
| 43 | + MessageBox.Show("SheesH"); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + //KeyboardHandler by tobre => https://stackoverflow.com/questions/60740328/keyboard-shortcut-does-not-make-it-to-cefsharp-browser-control |
| 50 | + public class KeyboardHandler : IKeyboardHandler |
| 51 | + { |
| 52 | + private bool _cefAltKeyPressed; |
| 53 | + public bool OnPreKeyEvent(IWebBrowser chromiumWebBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut) |
| 54 | + { |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + public bool OnKeyEvent(IWebBrowser chromiumWebBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey) |
| 59 | + { |
| 60 | + if (windowsKeyCode == 18) |
| 61 | + { |
| 62 | + _cefAltKeyPressed = true; |
| 63 | + } |
| 64 | + const int KEY_F4 = 115; |
| 65 | + //Console.WriteLine("Key Pressed " + windowsKeyCode); .> To Debug and analyse other Shortcut Combinations |
| 66 | + if (_cefAltKeyPressed) |
| 67 | + { |
| 68 | + if (windowsKeyCode == KEY_F4) |
| 69 | + { |
| 70 | + |
| 71 | + Application.Exit(); |
| 72 | + } |
| 73 | + |
| 74 | + _cefAltKeyPressed = false; |
| 75 | + |
| 76 | + } |
| 77 | + return true; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments