使用 C# 和 Xamarin 按 F8(播放)按钮后阻止 iTunes / Music.app 启动
Prevent iTunes / Music.app Start After Pressing F8(Play) Button with C# & Xamarin
众所周知,如果我们按下 F8(播放)键盘按钮,iTunes 或 Music .app 在 macOS 上默认打开。一些 Swift 类 可用于防止此键盘快捷方式,但它们与 C# 和 Xamarin 不兼容。例如,Spotify macOS 应用程序具有此功能。如果您按 UI 上的播放按钮一次,它会控制 iTunes 并处理 "Play" 按钮事件。
我有这个代码块。但是,由于 iTunes,macOS 无法触发代码块。字母和数字等其他键正常工作:
private NSEvent KeyboardEventHandler(NSEvent theEvent)
{
ushort keyCode = theEvent.KeyCode;
if(keyCode== 100) //rbKeyF8 100 is play button.
{
switch (isplaying)
{
case true:
StopMusic();
break;
case false:
PlayMusic();
break;
}
}
// NSAlert a = new NSAlert()
//{
// AlertStyle = NSAlertStyle.Informational,
// InformativeText = "You press the " + keyCode + " button :)",
// MessageText = "Hi"
//};
//a.RunModal();
return theEvent;
}
我们如何使用 C# 来实现?谢谢
答案是:
注册一个新的子类
[Register ("App")]
public class App : NSApplication
{
public App (System.IntPtr p) : base (p)
{
}
public override void SendEvent (NSEvent theEvent)
{
base.SendEvent (theEvent);
}
}
在 Info.plist 中更改 PrincipalClass
<key>NSPrincipalClass</key>
<string>App</string>
并且需要子类化 NSApplication 并查看 sendEvent。您应该将 Swift 代码转换为 C#。相关 swift 代码在这里:
Make my Cocoa app respond to the keyboard play/pause key?
Now, i have another problem. Yep, this code block solved my problem’s
first part. Now my app responds the play button press. However, iTunes
opened after the my app's respond. How can I prevent it to open ?
众所周知,如果我们按下 F8(播放)键盘按钮,iTunes 或 Music .app 在 macOS 上默认打开。一些 Swift 类 可用于防止此键盘快捷方式,但它们与 C# 和 Xamarin 不兼容。例如,Spotify macOS 应用程序具有此功能。如果您按 UI 上的播放按钮一次,它会控制 iTunes 并处理 "Play" 按钮事件。
我有这个代码块。但是,由于 iTunes,macOS 无法触发代码块。字母和数字等其他键正常工作:
private NSEvent KeyboardEventHandler(NSEvent theEvent)
{
ushort keyCode = theEvent.KeyCode;
if(keyCode== 100) //rbKeyF8 100 is play button.
{
switch (isplaying)
{
case true:
StopMusic();
break;
case false:
PlayMusic();
break;
}
}
// NSAlert a = new NSAlert()
//{
// AlertStyle = NSAlertStyle.Informational,
// InformativeText = "You press the " + keyCode + " button :)",
// MessageText = "Hi"
//};
//a.RunModal();
return theEvent;
}
我们如何使用 C# 来实现?谢谢
答案是: 注册一个新的子类
[Register ("App")]
public class App : NSApplication
{
public App (System.IntPtr p) : base (p)
{
}
public override void SendEvent (NSEvent theEvent)
{
base.SendEvent (theEvent);
}
}
在 Info.plist 中更改 PrincipalClass
<key>NSPrincipalClass</key>
<string>App</string>
并且需要子类化 NSApplication 并查看 sendEvent。您应该将 Swift 代码转换为 C#。相关 swift 代码在这里: Make my Cocoa app respond to the keyboard play/pause key?
Now, i have another problem. Yep, this code block solved my problem’s first part. Now my app responds the play button press. However, iTunes opened after the my app's respond. How can I prevent it to open ?