是否有可能在应用程序级别挂接到硬件键盘事件? [英] Is it possible to hook into Hardware keyboard events at an Application level?

查看:99
本文介绍了是否有可能在应用程序级别挂接到硬件键盘事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iOSAndroidUWP开发Xamarin.Forms应用程序.

I am developing a Xamarin.Forms application for iOS, Android and UWP.

我想在应用程序级别(而不是单个页面级别(因为它的Xamarin.Forms应用程序))陷入硬件键盘事件,以便用户按下键盘上的键时,可以在我的应用程序中调用一个动作应用程序.

I would like to hook into hardware keyboard events at an application level (not an individual page level (because its a Xamarin.Forms application)) so that when a user presses a key on the keyboard I can invoke an action in my application.

在Android上,我是在MainActivity上使用以下事件来完成此操作的.

On Android I did this using the following event on MainActivity:.

public override bool OnKeyUp([GeneratedEnum] Keycode keyCode, KeyEvent e)
{
    //process key press
    return base.OnKeyUp(keyCode, e);
}

Xamarin iOS是否等效?

推荐答案

您可以使用

You can use the KeyCommands to track the key pressing from hardware keyboard.

keyCommands

支持硬件键盘命令的响应者对象可以重新定义此属性,并使用它返回它支持的UIKeyCommand对象的数组.每个按键命令对象都代表要识别的键盘顺序以及响应者在响应中调用的操作方法.

A responder object that supports hardware keyboard commands can redefine this property and use it to return an array of UIKeyCommand objects that it supports. Each key command object represents the keyboard sequence to recognize and the action method of the responder to call in response.

从此方法返回的关键命令将应用于整个响应者链.当按下与键盘命令对象匹配的组合键时,UIKit会在响应程序链中移动,以寻找实现相应操作方法的对象.它在找到的第一个对象上调用该方法,然后停止处理事件.

The key commands you return from this method are applied to the entire responder chain. When an key combination is pressed that matches a key command object, UIKit walks the responder chain looking for an object that implements the corresponding action method. It calls that method on the first object it finds and then stops processing the event.

在Xamarin.forms中,您必须在iOS平台上为 ContentPage 创建自定义渲染器.然后,可以将keycommand添加到该页面渲染器中.

In Xamarin.forms, you have to create custom renderer for ContentPage at iOS platform. Then the keycommands can be added in that page renderer.

如果希望按键可以由ViewController(Page)处理,而不仅仅是输入控件(如Entry),请使用canBecomeFirstResponder使Viewcontroller成为第一响应者.

If you want the key presses can be handled by the ViewController(Page), not just the input controls(such as Entry), use canBecomeFirstResponder to enable viewcontroller to become a first responder.

例如,iOS平台中ContentPage的自定义呈现器可能是这样的:

using System;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using KeyCommandsInXamarinForms.iOS;

[assembly: ExportRenderer(typeof(ContentPage), typeof(MyCustomPageRenderer))]
namespace KeyCommandsInXamarinForms.iOS
{
    public class MyCustomPageRenderer : PageRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null || Element == null)
            {
                return;
            }

            //Create your keycommand as you need.
            UIKeyCommand keyCommand1 = UIKeyCommand.Create(new NSString("1"), UIKeyModifierFlags.Command, new ObjCRuntime.Selector("Action"));
            UIKeyCommand keyCommand2 = UIKeyCommand.Create(new NSString("\t"), 0, new ObjCRuntime.Selector("Action"));
            //Add your keycommands
            this.AddKeyCommand(keyCommand1);
            this.AddKeyCommand(keyCommand2);
        }

        [Export("Action:")]
        private void Excute(UIKeyCommand keyCommand)
        {
            Console.WriteLine(String.Format("key pressed - {0}", keyCommand.Value);
        }


        //Enable viewcontroller to become the first responder, so it is able to respond to the key commands.
        public override bool CanBecomeFirstResponder
        {
            get
            {
                return true;
            }
        }
    }
}

请注意此行,使用typeof(ContentPage)作为处理程序参数,则无需在PCL中进行任何更改:

Notice this line, using typeof(ContentPage) as the handler parameter, then you don't need to change anything in your PCL:

[assembly: ExportRenderer(typeof(ContentPage), typeof(MyCustomPageRenderer))]

这篇关于是否有可能在应用程序级别挂接到硬件键盘事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆