In XF3.0, most support platforms have physical keyboards like mac wpf gtk# and uwp. Android can also be connected to keyboard. So I need keydown keyup or keypress event, not only back button.
I need to monitor the shortcut keys like ctrl+A F1 etc.
Why not Android And iOS even if they are not connected to a physical keyboards?
I'm trying to do the same thing in a CustomRenderer, I hook up the KeyDown event like so but I recieve no call to the event despite trying tricks with focus to get the CustomRenderer to have focus. I feel like I'm missing something or its broke.
protected override void OnElementChanged(ElementChangedEventArgs
{
if (e.OldElement != null)
{
var oldController = (IFlowSKCanvasViewController)e.OldElement;
// unsubscribe from events
oldController.SurfaceInvalidated -= OnSurfaceInvalidated;
oldController.GetCanvasSize -= OnGetCanvasSize;
}
if (e.NewElement != null)
{
var newController = (IFlowSKCanvasViewController)e.NewElement;
// create the native view
if (Control == null)
{
var view = CreateNativeControl();
view.PaintSurface += OnPaintSurface;
SetNativeControl(view);
}
Control.KeyDown += Control_KeyDown;
}
Here Control_KeyDown if a breakpoint is inputted will never fire.
+1 here. I think this would be an important addition to any cross platform dev library!
Personally I want to detect a Ctrl-S (for Save) combination when a Bluetooth keyboard is attached to a mobile device.
+1.
I can't understand how OnKeyDown method isn't accesible from Xamarin.Form ContentPage.
Yeah, this needs to happen.
+1, Yes please. For the UWP version of my application, or an Android with keyboard, it would be great to detect keystrokes from the ContentPage.
At the moment this is what I have rigged up on a UWP custom renderer :
using BeforeOurTime.MobileApp.Pages.Explore;
using BeforeOurTime.MobileApp.UWP.Pages.Explore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Xamarin.Forms;
using Xamarin.Forms.Platform.UWP;
[assembly: ExportRenderer(typeof(ExplorePage), typeof(CustomExplorePageRenderer))]
namespace BeforeOurTime.MobileApp.UWP.Pages.Explore
{
public class CustomExplorePageRenderer : PageRenderer
{
/// <summary>
/// Monitor windows core ui keypress when ExplorePage is showing
/// </summary>
public CustomExplorePageRenderer() : base()
{
// When ExplorePage appears then attach to the ui core keydown event
Loaded += (object sender, RoutedEventArgs e) =>
{
Windows.UI.Core.CoreWindow.GetForCurrentThread().KeyDown += HandleKeyDown;
};
// When ExplorePage disappears then detach from the ui core keydown event
Unloaded += (object sender, RoutedEventArgs e) =>
{
Windows.UI.Core.CoreWindow.GetForCurrentThread().KeyDown -= HandleKeyDown;
};
}
/// <summary>
/// Forward a keypress to an ExplorePage method
/// </summary>
/// <param name="window"></param>
/// <param name="e"></param>
public void HandleKeyDown(Windows.UI.Core.CoreWindow window, Windows.UI.Core.KeyEventArgs e)
{
(Element as ExplorePage).KeyPressed(Element, new KeyEventArgs { Key = e.VirtualKey.ToString() });
}
}
}
Note: ExplorePage is checking to see if any child control has focus before processing the key down!
Presumably the right way to do this is to allow a listener to be attached to any View
, similarly to touch?
It would also be nice if gesture event handlers also told you which keyboard buttons were pressed at the time of the gesture. You could use this to cause a click to behave differently from a CTRL+click. However, if key pressed events (or keyup+keydown) were implemented, you could use that to do it yourself.
I'm stuck at not able to get a event when an entry is empty, and delete is pressed, i hope we can add keyboard events to views
Let me add my 2 cents.
How about to have such a thing as KeyboardGestureRecognizer?
are we still considering this @samhouts ?
Yes, we are still considering this. What would the Xamarin.Forms API look like? Can you show examples of how to use it with C# and XAML? Thanks!
OnKeyUp (Down) event for ContentPage would be ok.
I would like more something like this one:
```xaml
AndAlt="True"
AndCtrl="False"
AndShift="True"
Command="{Binding MyCommand}"
Event="..."/>
@samhouts Anything about this ?
Most helpful comment
OnKeyUp (Down) event for ContentPage would be ok.
Key="X"
I would like more something like this one:
```xaml
AndAlt="True"
AndCtrl="False"
AndShift="True"
Command="{Binding MyCommand}"
Event="..."/>