'How do I implement a global keyboard hook using GTk2 C# Visual Studio on mac?
This is not a duplicate question. I have read lots of implementations of low level / global keyboard hooks using C# but none of them have yet provided a solution for MacOSX Visual Studio. Basically, I have a GTK2 .net MAC application. I want (the application) to detect whenever the space key is pressed, whether the application is on focus or not. I have successfully implemented a simple keyboard hook on a console app using C# (for Mac) but my console application would not detect the global keyboard events when the application is not in focus. Even when my current application is not in focus, I want it to detect it whenever the space key is pressed. How do I do this ? Here is my MainWindow.cs
using System;
using Gtk;
using System.IO.Ports;
using System.Threading;
public partial class MainWindow : Gtk.Window
{
// MySQL to fetch visitors number
//Create a MySQL connection
int visitors = 0;
public MainWindow() : base(Gtk.WindowType.Toplevel)
{
Build();
}
protected void OnDeleteEvent(object sender, DeleteEventArgs a)
{
Application.Quit();
a.RetVal = true;
}
public void DetectWhenSpaceKeyIsPressed(){
if(//space key is pressed -low level keyboard hook mac OSX){
//I send the data
}
}
}
Solution 1:[1]
I have implemented this function for a console app for .net core in MacOS
if (!Console.KeyAvailable) return null;
ConsoleKeyInfo c = Console.ReadKey(true);
switch (c.Key)
{
case ConsoleKey.UpArrow:
//go up
break;
case ConsoleKey.DownArrow:
//go down
break;
... etc
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | DanielV |