Problem; window.external is null for child windows
I have a WinForms application that contains a CEFSharp Chromium browser control on the main form. The complete code is provided here; note this is not my application but a much simpler example that shows the problem.
public partial class Form1 : Form
{
private WebInteropAPI _objForScripting;
public Form1()
{
InitializeComponent();
Load += OnFormLoaded;
_objForScripting = new WebInteropAPI();
}
private void OnFormLoaded(object sender, EventArgs e)
{
SetupBrowser();
_browser.Load("http://localhost:80");
}
private void SetupBrowser()
{
_browser.JavascriptObjectRepository.ResolveObject += ResolveObject;
_browser.FrameLoadStart += FrameLoadStart;
}
private void FrameLoadStart(object sender, FrameLoadStartEventArgs e)
{
_browser.ExecuteScriptAsync("CefSharp.DeleteBoundObject(\"external\"); CefSharp.RemoveObjectFromCache(\"external\"); CefSharp.BindObjectAsync(\"external\");");
}
private void ResolveObject(object sender, JavascriptBindingEventArgs e)
{
var repo = e.ObjectRepository;
if (e.ObjectName == "external" && repo != null && _objForScripting != null)
{
BindingOptions options = BindingOptions.DefaultBinder;
options.CamelCaseJavascriptNames = false;
try
{
repo.Register("external", _objForScripting, true, options);
}
catch (Exception exception)
{
Console.WriteLine(exception);
throw;
}
}
}
}
Here is the WebInteropAPI class for completeness
public class WebInteropAPI
{
public string Push(string name, string payload)
{
Debug.WriteLine("#### Push() name:" + name + " payload:" + payload + " ####");
return payload;
}
public void PushData(string payload)
{
Debug.WriteLine("#### PushData() payload:" + payload + " ####");
}
}
On form load I register my interop and navigate the browser to a local HTML page on my machine; it looks as follows
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="//code.jquery.com/jquery-1.8.3.js"></script>
</head>
<body>
<p>
<button onclick="myFunction()">Invoke Push Data on Host</button>
Launch child iFrame
</p>
<script type="text/javascript">
$('.open-popup').click(function(e) {
e.preventDefault();
window.open(this.href, '_blank', 'width=1000,height=750');
});
</script>
<script>
function myFunction() {
window.external.Push("This is the name property","This is the payload data property");
}
</script>
</body>
</html>
Running the application results in the rather fetching UI
Clicking the button does indeed invoke the method on my WebInteropAPI
However; clicking the link to open a new child window and then clicking the button results in an error
Any help is greatly appreciated.
UPDATE; I've found something that works but I'm unsure if it's good or bad from a standards point of view.
My updated Forms view now looks like;
public partial class Form1 : Form
{
private FrameHelper _frameHelper;
private JSObjectHelper _jsObjectHelper;
public Form1()
{
InitializeComponent();
Load += OnFormLoaded;
}
private void OnFormLoaded(object sender, EventArgs e)
{
SetupBrowser();
_browser.Load("http://localhost:80");
}
private void SetupBrowser()
{
_jsObjectHelper = new JSObjectHelper(new WebInteropAPI());
_frameHelper = new FrameHelper();
_browser.JavascriptObjectRepository.ResolveObject += (s, a) =>
{
_jsObjectHelper.ResolveObject(a);
};
_browser.FrameLoadStart += (s, a) =>
{
_frameHelper.FrameLoadStart(_browser);
};
_browser.LifeSpanHandler = new LifeSpanHandler(_frameHelper, _jsObjectHelper);
}
}
The magic now happens in the LifeSpanHandler
public class LifeSpanHandler : ILifeSpanHandler
{
private readonly FrameHelper _frameHelper;
private readonly JSObjectHelper _jsObjectHelper;
private ChromiumWebBrowser _browser;
public LifeSpanHandler(FrameHelper frameHelper, JSObjectHelper jsObjectHelper)
{
_frameHelper = frameHelper;
_jsObjectHelper = jsObjectHelper;
}
public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
return false;
}
public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser){}
public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser){}
public bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
_browser = new ChromiumWebBrowser(string.Empty);
_browser.Bounds = ((ChromiumWebBrowser)chromiumWebBrowser).Bounds;
_browser.BrowserSettings = ((ChromiumWebBrowser)chromiumWebBrowser).BrowserSettings;
_browser.FrameLoadStart += (s, a) => { _frameHelper.FrameLoadStart(_browser); };
_browser.JavascriptObjectRepository.ResolveObject += (s, a) => { _jsObjectHelper.ResolveObject(a); };
newBrowser = _browser;
return false;
}
}
And for completeness; here's the frame helper and the JsObjectHelper
public class FrameHelper
{
public void FrameLoadStart(IWebBrowser browser)
{
browser.ExecuteScriptAsync("CefSharp.DeleteBoundObject(\"external\"); CefSharp.RemoveObjectFromCache(\"external\"); CefSharp.BindObjectAsync(\"external\");");
}
}
public class JSObjectHelper
{
private readonly WebInteropAPI _objForScripting;
public JSObjectHelper(WebInteropAPI objForScripting)
{
_objForScripting = objForScripting;
}
public WebInteropAPI ObjForScripting { get; }
public void ResolveObject(JavascriptBindingEventArgs e)
{
var repo = e.ObjectRepository;
if (e.ObjectName == "external" && repo != null && _objForScripting != null)
{
BindingOptions options = BindingOptions.DefaultBinder;
options.CamelCaseJavascriptNames = false;
try
{
repo.Register("external", _objForScripting, true, options);
}
catch
{
}
}
}
}
Related
I am trying to create a rdp in WPF. I used the AxMSTSCLib library to create a rdp client in my wpf application. I planned to create a usercontrol that hosts the rdp.I achieved it with the following code.
But i get the grey area of the windows host visible. I need to fill the rdp client for the size of the windows host.
public partial class RDPUserControl : UserControl
{
internal RDPActiveXControl _rdp;
internal string Servername { get; set; }
internal string Username { get; set; }
internal string Password { get; set; }
internal TabItem TabItem { get; set; }
public RDPUserControl()
{
InitializeComponent();
}
private void InitData()
{
_rdp = new RDPActiveXControl();
((System.ComponentModel.ISupportInitialize)(_rdp)).BeginInit();
_rdp.Name = "rdp";
_rdp.Enabled = true;
wfHost.Child = _rdp;
((System.ComponentModel.ISupportInitialize)(_rdp)).EndInit();
}
internal void Connect()
{
_rdp.Server = Servername;
_rdp.UserName = Username;
_rdp.AdvancedSettings7.ClearTextPassword = Password;
_rdp.ColorDepth = 24;
_rdp.AdvancedSettings7.SmartSizing = true;
_rdp.AdvancedSettings7.AuthenticationLevel = 2;
_rdp.AdvancedSettings7.EnableCredSspSupport = true;
_rdp.Width = Convert.ToInt32(this.ActualWidth);
_rdp.Height = Convert.ToInt32(this.ActualHeight);
_rdp.DesktopWidth = Convert.ToInt32(this.ActualWidth);
_rdp.DesktopHeight = Convert.ToInt32(this.ActualHeight);
_rdp.OnDisconnected += _rdp_OnDisconnected;
try
{
_rdp.Connect();
}
catch(Exception e)
{
MessageBox.Show("Connection Failed: "+e.Message);
}
}
private void _rdp_OnDisconnected(object sender, AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEvent e)
{
MainWindow window = Application.Current.MainWindow as MainWindow;
Page page = window._mainFrame.Content as Page;
ConnectionsPage connectionPage = page as ConnectionsPage;
connectionPage.CloseTab(TabItem);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
InitData();
Connect();
}
public class RDPActiveXControl : AxMSTSCLib.AxMsRdpClient6NotSafeForScripting
{
public RDPActiveXControl() : base() { }
protected override void WndProc(ref System.Windows.Forms.Message m)
{
// Fix for the missing focus issue on the rdp client component
if (m.Msg == 0x0021) // WM_MOUSEACTIVATE
{
if (!this.ContainsFocus)
{
this.Focus();
}
}
base.WndProc(ref m);
}
}
How to make the RDP ActiveXControl for Full size of the windowsFormHost
I tried with
_rdp.Dock = System.Windows.Forms.DockStyle.Fill;
But it makes the app to disappear.Anyone know what is wrong with this?. Any working example will be helpful.
I'm trying to get the direction of the upcoming turn while travelling, i.e. I want to trigger an event in my app according to the direction of the upcoming turn.
I've tried using event listeners, taking help of the documentation and the provided examples but as I'm pretty new to android studio and mapbox, I've not been successful (my app either crashed or the function would never get triggered). I've also tried searching for getting the voice commands into text form or log form but have failed.
While my current code does display directions and gives voiced instructions, I can't figure out how to access either of them. I'd like to know if there's a simple way of achieving what I'm after without using any event listeners.
private MapView mapView;
private MapboxMap mapboxMap;
private PermissionsManager permissionsManager;
private LocationComponent locationComponent;
private DirectionsRoute currentRoute;
private static final String TAG = "DirectionsActivity";
private NavigationMapRoute navigationMapRoute;
private MapboxNavigation navigation;
private Button button;
private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
// Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
}
#Override
public void onMapReady(#NonNull final MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
//Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
mapboxMap.setStyle(getString(R.string.navigation_guidance_day), new Style.OnStyleLoaded() {
#Override
public void onStyleLoaded(#NonNull Style style) {
enableLocationComponent(style);
addDestinationIconSymbolLayer(style);
mapboxMap.addOnMapClickListener(MainActivity.this);
button = findViewById(R.id.startButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean simulateRoute = true;
NavigationLauncherOptions options = NavigationLauncherOptions.builder()
.directionsRoute(currentRoute)
.shouldSimulateRoute(simulateRoute)
.build();
NavigationLauncher.startNavigation(MainActivity.this, options);
}
});
}
});
}
private void addDestinationIconSymbolLayer(#NonNull Style loadedMapStyle) {
loadedMapStyle.addImage("destination-icon-id",
BitmapFactory.decodeResource(this.getResources(), R.drawable.mapbox_marker_icon_default));
GeoJsonSource geoJsonSource = new GeoJsonSource("destination-source-id");
Log.d(TAG, "addDestinationIconSymbolLayer: " + geoJsonSource);
loadedMapStyle.addSource(geoJsonSource);
SymbolLayer destinationSymbolLayer = new SymbolLayer("destination-symbol-layer-id", "destination-source-id");
destinationSymbolLayer.withProperties(
iconImage("destination-icon-id"),
iconAllowOverlap(true),
iconIgnorePlacement(true)
);
loadedMapStyle.addLayer(destinationSymbolLayer);
}
#SuppressWarnings( {"MissingPermission"})
#Override
public boolean onMapClick(#NonNull LatLng point) {
Point destinationPoint = Point.fromLngLat(point.getLongitude(), point.getLatitude());
Point originPoint = Point.fromLngLat(locationComponent.getLastKnownLocation().getLongitude(),
locationComponent.getLastKnownLocation().getLatitude());
GeoJsonSource source = mapboxMap.getStyle().getSourceAs("destination-source-id");
Log.d(TAG, "Does this even work");
Log.d(TAG, "onMapClick: " + source.toString());
if (source != null) {
source.setGeoJson(Feature.fromGeometry(destinationPoint));
}
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
return true;
}
private void getRoute(Point origin, Point destination) {
NavigationRoute.builder(this)
.accessToken(Mapbox.getAccessToken())
.origin(origin)
.destination(destination)
.build()
.getRoute(new Callback<DirectionsResponse>() {
#Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
Log.d(TAG, "Response code: " + response.code());
if (response.body() == null) {
Log.e(TAG, "No routes found, make sure you set the right user and access token.");
return;
} else if (response.body().routes().size() < 1) {
Log.e(TAG, "No routes found");
return;
}
currentRoute = response.body().routes().get(0);
if (navigationMapRoute != null) {
navigationMapRoute.removeRoute();
} else {
navigationMapRoute = new NavigationMapRoute(null, mapView, mapboxMap, R.style.NavigationMapRoute);
}
navigationMapRoute.addRoute(currentRoute);
}
#Override
public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
Log.e(TAG, "Error: " + throwable.getMessage());
}
});
}
#SuppressWarnings( {"MissingPermission"})
private void enableLocationComponent(#NonNull Style loadedMapStyle) {
if (PermissionsManager.areLocationPermissionsGranted(this)) {
locationComponent = mapboxMap.getLocationComponent();
locationComponent.activateLocationComponent(this, loadedMapStyle);
locationComponent.setLocationComponentEnabled(true);
locationComponent.setCameraMode(CameraMode.TRACKING);
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
#Override
public void onExplanationNeeded(List<String> permissionsToExplain) {
Toast.makeText(this, R.string.user_location_permission_explanation, Toast.LENGTH_LONG).show();
}
#Override
public void onPermissionResult(boolean granted) {
if (granted) {
enableLocationComponent(mapboxMap.getStyle());
} else {
Toast.makeText(this, R.string.user_location_permission_not_granted, Toast.LENGTH_LONG).show();
finish();
}
}
// Add the mapView's own lifecycle methods to the activity's lifecycle methods
#Override
public void onStart() {
super.onStart();
mapView.onStart();
}
#Override
public void onResume() {
super.onResume();
mapView.onResume();
// Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
}
#Override
public void onPause() {
super.onPause();
mapView.onPause();
}
#Override
public void onStop() {
super.onStop();
mapView.onStop();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
#Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
It sounds like you might want to look at using an event listener for a custom milestone. Here's a link to the docs:
https://docs.mapbox.com/android/navigation/overview/milestones/#milestone-event-listener
I want to insert html code into existing html code.
But I do not see the result. Here is the code C #:
1) Program.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
wUI.DocumentReady += wUI_DocumentReady;
}
private void Form1_Load(object sender, EventArgs e)
{
// code here ?
}
void wUI_DocumentReady(object sender, DocumentReadyEventArgs e)
{
wUI.LoadHTML("<html><body>sadasdsad</body></html>");
HtmlManager html = HtmlManager.Instance;
string[] placeholders = { "asset://customdatastore/path/to/any", "type-button", "no-action", "Example link" };
html.Add("{3}", placeholders);
html.InnerCode(html.Code, wUI, "body");
wUI.Refresh();
}
}
2) HtmlManager.cs
public sealed class HtmlManager
{
private static readonly Lazy<HtmlManager> InstanceField = new Lazy<HtmlManager>(() => new HtmlManager());
private StringBuilder _stringBuilder = null;
public string Code { get { return _stringBuilder.ToString(); } }
private HtmlManager()
{
if (_stringBuilder != null)
_stringBuilder.Clear();
_stringBuilder = new StringBuilder();
}
public static HtmlManager Instance { get { return InstanceField.Value; } }
public void Add(string row, string[] placeholders = null)
{
if (placeholders != null)
_stringBuilder.AppendLine(string.Format(row, placeholders));
_stringBuilder.AppendLine(row);
}
public void InnerCode(string code, object sender, string afterTag = "html")
{
Awesomium.Windows.Forms.WebControl ui = (Awesomium.Windows.Forms.WebControl)sender;
ui.ExecuteJavascript(string.Format("document.getElementsByTagName({0})[0].innerHTML({1})", afterTag, code));
}
public void Clear()
{
_stringBuilder.Clear();
}
}
The event (DocumentReady) does not happen, I do not believe, maybe I'm wrong somewhere?
UP: I try do it:
private void Form1_Load(object sender, EventArgs e)
{
wUI.LoadHTML("<html><body>sadasdsad</body></html>");
}
void wUI_DocumentReady(object sender, DocumentReadyEventArgs e)
{
HtmlManager html = HtmlManager.Instance;
string[] placeholders = { "asset://customdatastore/path/to/any", "type-button", "no-action", "Example link" };
html.Add("{3}", placeholders);
wUI.ExecuteJavascript("document.getElementsByTagName('body').innerHTML(\"sometext\")");
//html.InnerCode(html.Code, wUI, "body");
//wUI.Refresh();
}
No result
UP 2:
public void Add(string row, string[] placeholders = null)
{
if (placeholders != null)
_stringBuilder.AppendLine(string.Format(row, placeholders));
if (placeholders == null)
_stringBuilder.AppendLine(row);
}
UP 3:
Work with:
wUI.Source = new Uri(#"http://google.com");
in Form1_Load
You can use LoadHtml method, but only after document is fully loaded (don't confuse with DocumentReadyState.Ready) It works for me at least:
private void WebControl_DocumentReady(object sender, DocumentReadyEventArgs e)
{
if (e.ReadyState != DocumentReadyState.Loaded) return;
}
But as an initialisation, you should use Source property, like you wrote in your third update
I am implementing a Webkit Browser control in my windows app.
I need to use a custom context menu (right click) that only has copy/cut/paste as its options regardless of what element is right clicked. I need kind of a step-by-step as to how to implement it
Customizing the context menu for the WebKitBrowser supposes that you get a reference to the WebViewClass and then, setting a IWebUIDelegate for it by calling the setUIDelegate() method.
void MyWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var webView = this.GetWebView() as WebKit.Interop.WebViewClass;
webView.setUIDelegate(new MyWebUIDelegate(this));
}
In the IWebUIDelegate implementation you may intercept the contextMenuItemsForElement method and trigger the display of the context menu of the browser.
Here is a working sample:
public partial class Form1 : Form
{
MyWebBrowser webKitBrowser;
public Form1()
{
InitializeComponent();
webKitBrowser = new MyWebBrowser();
webKitBrowser.Dock = DockStyle.Fill;
this.Controls.Add(webKitBrowser);
webKitBrowser.Navigate("http://www.google.com");
}
}
class MyContextMenu : ContextMenu
{
public MyContextMenu()
{
var cutMenuItem = new MenuItem("Cut");
var copyMenuItem = new MenuItem("Copy");
var pasteMenuItem = new MenuItem("Paste");
cutMenuItem.Click += cutMenuItem_Click;
MenuItems.Add(cutMenuItem);
MenuItems.Add(copyMenuItem);
MenuItems.Add(pasteMenuItem);
}
void cutMenuItem_Click(object sender, EventArgs e)
{
//TODO: implement functionality
MessageBox.Show("Cut was selected");
}
}
class MyWebBrowser : WebKitBrowser
{
public event EventHandler ShowContextMenu = new EventHandler(OnFireShowContextMenu);
public MyWebBrowser()
{
DocumentCompleted += MyWebBrowser_DocumentCompleted;
var myContextMenu = new MyContextMenu();
ContextMenu = myContextMenu;
}
void MyWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var webView = this.GetWebView() as WebKit.Interop.WebViewClass;
webView.setUIDelegate(new MyWebUIDelegate(this));
}
public static void OnFireShowContextMenu(object sender, EventArgs e)
{
var webBrowser = (Control)sender;
var webView = (WebKit.Interop.WebViewClass)((MyWebBrowser)sender).GetWebView();
var originalPoint = webBrowser.PointToScreen(new Point(0, 0));
var currentPoint = new Point(Cursor.Position.X - originalPoint.X, Cursor.Position.Y - originalPoint.Y);
((WebKitBrowser)sender).ContextMenu.Show((Control)sender, currentPoint);
}
public void FireShowContextMenu()
{
this.ShowContextMenu(this, null);
}
}
class MyWebUIDelegate : IWebUIDelegate
{
private MyWebBrowser owner;
public MyWebUIDelegate(MyWebBrowser browser)
{
this.owner = browser;
}
//trigger the browser's FireShowContextMenu() method
public int contextMenuItemsForElement(WebView sender, CFDictionaryPropertyBag element, int defaultItemsHMenu)
{
owner.FireShowContextMenu();
return defaultItemsHMenu;
}
//return 1, true
public int hasCustomMenuImplementation()
{
return 1;
}
//the rest of the IWebUIDelegate interface implementation
}
For more insight, probably you would want to study some other customizations, such as open-webkit-sharp.
How do I capture a key down event in WPF even if my application is not focused?
For me, the best way is this:
public MainWindow()
{
InitializeComponent();
CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
}
void CompositionTarget_Rendering(object sender, EventArgs e)
{
if ((Keyboard.GetKeyStates(Key.W) & KeyStates.Down) > 0)
{
player1.walk();
}
}
The rendering event runs every time.
Global keyboard hook can slow down your debugging.
I prefer to use this approach:
Create KeyboardListener class
public class KeyboardListener : IDisposable
{
private readonly Thread keyboardThread;
//Here you can put those keys that you want to capture
private readonly List<KeyState> numericKeys = new List<KeyState>
{
new KeyState(Key.D0),
new KeyState(Key.D1),
new KeyState(Key.D2),
new KeyState(Key.D3),
new KeyState(Key.D4),
new KeyState(Key.D5),
new KeyState(Key.D6),
new KeyState(Key.D7),
new KeyState(Key.D8),
new KeyState(Key.D9),
new KeyState(Key.NumPad0),
new KeyState(Key.NumPad1),
new KeyState(Key.NumPad2),
new KeyState(Key.NumPad3),
new KeyState(Key.NumPad4),
new KeyState(Key.NumPad5),
new KeyState(Key.NumPad6),
new KeyState(Key.NumPad7),
new KeyState(Key.NumPad8),
new KeyState(Key.NumPad9),
new KeyState(Key.Enter)
};
private bool isRunning = true;
public KeyboardListener()
{
keyboardThread = new Thread(StartKeyboardListener) { IsBackground = true };
keyboardThread.Start();
}
private void StartKeyboardListener()
{
while (isRunning)
{
Thread.Sleep(15);
if (Application.Current != null)
{
Application.Current.Dispatcher.Invoke(() =>
{
if (Application.Current.Windows.Count > 0)
{
foreach (var keyState in numericKeys)
{
if (Keyboard.IsKeyDown(keyState.Key) && !keyState.IsPressed) //
{
keyState.IsPressed = true;
KeyboardDownEvent?.Invoke(null, new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromDependencyObject(Application.Current.Windows[0]), 0, keyState.Key));
}
if (Keyboard.IsKeyUp(keyState.Key))
{
keyState.IsPressed = false;
}
}
}
});
}
}
}
public event KeyEventHandler KeyboardDownEvent;
/// <summary>
/// Состояние клавиши
/// </summary>
private class KeyState
{
public KeyState(Key key)
{
this.Key = key;
}
public Key Key { get; }
public bool IsPressed { get; set; }
}
public void Dispose()
{
isRunning = false;
Task.Run(() =>
{
if (keyboardThread != null && !keyboardThread.Join(1000))
{
keyboardThread.Abort();
}
});
}
}
Subscribe to KeyboardDownEvent in code-behind (or where you need it).
public partial class MainWindow : Window
{
private KeyboardListener listener;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
listener = new KeyboardListener();
listener.KeyboardDownEvent += ListenerOnKeyPressed;
}
private void ListenerOnKeyPressed(object sender, KeyEventArgs e)
{
// TYPE YOUR CODE HERE
}
private void Window_OnUnloaded(object sender, RoutedEventArgs e)
{
listener.KeyboardDownEvent -= ListenerOnKeyPressed;
}
}
Done
See this questions for hooking the keyboard Using global keyboard hook (WH_KEYBOARD_LL) in WPF / C#