[英]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.
我已经阅读了许多使用 C# 的低级/全局键盘挂钩的实现,但它们都没有为 MacOSX Visual Studio 提供解决方案。 Basically, I have a GTK2 .net MAC application.
基本上,我有一个 GTK2 .net MAC 应用程序。 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.
我已经使用 C#(适用于 Mac)在控制台应用程序上成功实现了一个简单的键盘挂钩,但是当应用程序不在焦点上时,我的控制台应用程序不会检测到全局键盘事件。 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
这是我的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
}
}
}
I have implemented this function for a console app for .net core in MacOS我已经在 MacOS 中为 .net 核心的控制台应用程序实现了这个功能
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.