Open On-Screen Keyboard for Xamarin/Monogame - c#

I'm developing a game using Xamarin/Monogame and I need to open the keyboard on a mobile device when they click on my input control. I recognize that I can capture input using Keyboard.GetState() when I'm using the emulator and my keyboard, but real users will have physical devices, and I need to open the on-screen keyboard for them to enter in information. I don't see anything in any documentation describing this, but it's hard to believe I'm the first person to run into this. I've looked through Xamarin/Monogame's docs on input and I've read this StackOverflow article. Let me know if you need more information to help solve this. Thanks in advance for any help.

After much digging I was able to figure out the solution. I'll first start by explaining how I got there:
I stopped thinking "how do I display the on-screen keyboard in Mono?" and started thinking "how do you display the keyboard on an Android device?" This lead me to this article which was perfect; Xamarin and Android. Here's the relevant snippets:
Showing the soft input (on-screen keyboard)
var pView = game.Services.GetService<View>();
var inputMethodManager = Application.GetSystemService(Context.InputMethodService) as InputMethodManager;
inputMethodManager.ShowSoftInput(pView, ShowFlags.Forced);
inputMethodManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
Some key notes about this:
The View class is Android.Views.View
The InputMethodManager class is Android.Views.InputMethods.InputMethodManager
This code is called from within an Activity class, so Application refers to a property on that class (this.Application)
game is your game class (in Mono) that is of type Microsoft.Xna.Framework.Game
Hiding the soft input (on-screen keyboard)
InputMethodManager inputMethodManager = Application.GetSystemService(Context.InputMethodService) as InputMethodManager;
inputMethodManager.HideSoftInputFromWindow(pView.WindowToken, HideSoftInputFlags.None);
After I solved this, the way I was capturing input (the XNA/Mono way) was not capturing this input. So I had to continue digging. While it doesn't necessarily pertain to this question, I'd like to post it here so it exists somewhere. This documentation on the Android Developers site helped me.
To capture input directly (which is what we need to do to capture to OSK input), you need to override the OnKeyPress method on the activity your game is running in. For me, this looks as follows:
public class AndroidActivity : AndroidGameActivity
{
private void OnKeyPress(object sender, View.KeyEventArgs e)
{
// do stuff with the input
}
}
Hope this helps anyone else who got stuck with this; the information was hard to find, but now that I've changed my frame of mind to "how does Android/iOS handle this thing" I've been able to locate answers more easily. Good luck!

Related

Xamarin.Mac Secure Text Field to move the focus by inputting Enter key

I would like to move the focus by input enter key after entering a string into a secure text field, but I have no idea how to do it at all.
Do I define it as an Outlet? What do I do then?
I couldn't find anything on Google the following code in fragments only:
public override void ViewDidLoad()
{
base.ViewDidLoad();
textPassword.ShouldReturn = (NSSecureTextField) =>
{
textPassword.ResignFirstResponder();
return true;
};
}
Of course, it doesn't work. What do I need to do to make this work?
In order to do what you are trying to achieve you will want to go into your storyboard and right click on both your NSTextFields individually.
This will bring up a list of methods that can be utilised
see screenshot
Dragging action into your viewcontroller.h file within xcode will set up the action link for both textfields.
Now in order to link this to your Viewcontroller.cs youll need to add the methods you set up into your viewcontroller.See screenshot
The code you found on google was close however it seems in order to resignfirstresponder on mac you need to assign first responder to something else.
Looking at the screenshot above i've taken first responder from the emailtextfield(1) and given it to passwordtextfield(2) which seems to be similar to what your looking to do.
Let me know how you get on.
Rob

How do I force selection of a keyboard type in a Xamarin.Webkit.Webview

We have a Xamarin app (Android) that at one stage opens up a web view (Webkit.Webview not Forms.Webview). This directs the user to a page on a third party site which has been set up for us.
Firstly - on certain input fields the keyboard which shows up is the wrong one - we are expecting a dismissable keyboard (i.e. "Done" in the bottom corner, not a "Submit"). I know this can be changed but not sure what is the correct way to do this. Does it have to be the metadata/text inputs on the web page that is changed? If so - what needs to be modified per text box entry on the html of the page? Just the type? i.e:
<input type="email">
Secondly, rather than wait for the third party to fix the page, is there a way we can force the webview to always open a certain keyboard type?
We have an option of intercepting the keyboard key presses and trying to dismiss the keyboard on return press at the minute. But would prefer not to put a hack in that intercepts every key press.
Appreciate the help, not sure what the way forward is here.
Thanks
From the comments: To your second question about forcing a keyboard button, you can check out this link which describes how to override OnCreateInputConnection to specify the Keyboard Enter Button type.
public class MyWebView : WebView {
...
public override IInputConnection OnCreateInputConnection (EditorInfo outAttrs) {
var inputConnection = base.OnCreateInputConnection (outAttrs);
// outAttrs.ImeOptions in Xamarin only allows ImeFlags but it also should allow ImeActions
outAttrs.ImeOptions = outAttrs.ImeOptions | (ImeFlags)ImeAction.Next;
return inputConnection;
}
}
That will not dismiss your keyboard when tapped though since it is meant to take the user to the next input. Hopefully someone else can come along and either provide a better answer or give a good way to dismiss the keyboard in this situation without hacking something together.

how to change language of Virtual Keyboard in C# or VB?

I want to change language of OSK in my Project when show.
How can I do it?
This is my code:
Private oskProcess As Process
oskProcess = Process.Start("osk")
I think OSK can't do this, but TabTip can.
Maybe this will help you.
Private tabtipProcess As Process
tabtipProcess = Process.Start("tabtip")
and read this article, tells about how to control tabtip. It may be usefull Start TabTip with numpad view open

count any keystrokes whether or not Window (App) is in focus - WPF

I'm building a "WPF Application" which is made to be run in the background (minimised state) and detects KeyStrokes of each and Every key on the keyboard & every Mouse Clicks.
So, my question is how to detect every keyStrokes whether app (Window) is minimised or not.
Simply, if my app is in focus then i use this code to count keystrokes.
Public int count;
protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
{
//base.OnKeyDown(e);
count++;
tBlockCount.Text = count.ToString();
}
I just want to do the same even if my app is minimised.
I've searched a lot and come across many suggestions like..
http://www.pinvoke.net/default.aspx/user32/registerhotkey.html
http://social.msdn.microsoft.com/Forums/vstudio/en-US/87d66b1c-330c-42fe-8a40-81f82012575c/background-hotkeys-wpf?forum=wpf
Detecting input keystroke during WPF processing
Detect if any key is pressed in C# (not A, B, but any)
Most of those are indicating towards Registering HotKeys. But I'm unable to match scenario with mine.
Any kind of suggestion are most welcome.
Although I'm not really condoning the use of a keylogger (This is what you are trying to do). I would recommend taking a look at this q/a, the section near the bottom of this article, and this article for some inspiration. These should help point in the right direction for the coding side.
What you essentially need to do is just set up an event to intercept any keys that come in from the computer, then you can gather the key and do whatever you like with it (in your case, record it)
Edit: In fact, reading the third article, it actually gives a full code snippet on how to implement and use it in WPF, so I recommend just reading that one.

XNA show xbox onscreen keyboard in PC

I need to show, and input some text in xbox-like onscreen keyboard. Sadly, when I call Guide.BeginShowKeyboardInput, there is only some funny textbox shown, and i must fill it via keyboard. I know, that on PC iv very normal to use keyboard, but in my case i MUST enter text via gamepad, using xbox on screen keyboard.
Is there any way to achieve this? To call xbox onscreen keyboard on PC?
If you need one for like a touchscreen monitor you could do
using System.Diagnostics;
using System.IO;
then use this function
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.System) + Path.DirectorySeparatorChar + "osk.exe");
but if you need it to work with like an xbox controller you will probably need to build your own
No. It was a design decision (documented here) to give the end user control of the keyboard being invoked. Therefore, the end user has to touch a text box (or the like) to invoke the virtual on-screen keyboard.
Check this text form this link
Blockquote User-driven invocation
The invocation model of the touch keyboard is designed to put the user in control of the keyboard. Users indicate to the system that they want to input text by tapping on an input control instead of having an application make that decision on their behalf. This reduces to zero the scenarios where the keyboard is invoked unexpectedly, which can be a painful source of UI churn because the keyboard can consume up to 50% of the screen and mar the application's user experience. To enable user-driven invocation, we track the coordinates of the last touch event and compare them to the location of the bounding rectangle of the element that currently has focus. If the point is contained within the bounding rectangle, the touch keyboard is invoked.
Blockquote This means that applications cannot programmatically invoke the touch keyboard via manipulation of focus. Big culprits here in the past have been webpages—many of them set focus by default into an input field but have many other experiences available on their page for the user to enjoy. A good example of this is msn.com. The website has a lot of content for consumption, but happens to have a Bing search bar on the top of its page that takes focus by default. If the keyboard were automatically invoked, all of the articles located below that search bar would be occluded by default, thus ruining the website's experience on tablets. There are certain scenarios where it doesn't feel great to have to tap to get the keyboard, such as when a user has started a new email message or has opened the Search pane. However, we feel that requiring the user to tap the input field is an acceptable compromise.
Check out: http://classes.soe.ucsc.edu/cmps020/Winter08/lectures/controller-keyboard-input.pdf
You might find your answer in here, It has all information about the input of a gamepad and such.
There is a good guide on how to do this here:
static public string GetKeyboardInput()
{
if (HandleInput.currentState.IsButtonDown(Buttons.B))
{
useKeyboardResult = false;
}
if (KeyboardResult == null && !Guide.IsVisible)
{
string title = "Name";
string description = "Pick a name for this game";
string defaultText = "Your name here";
pauseType = PauseType.pauseAll;
KeyboardResult = Guide.BeginShowKeyboardInput(HandleInput.playerIndex, title,
description, defaultText, null, null);
useKeyboardResult = true;
pauseType = PauseType.pauseAll;
}
else if (KeyboardResult != null && KeyboardResult.IsCompleted)
{
pauseType = PauseType.none;
KeyboardInputRquested = false;
string input = Guide.EndShowKeyboardInput(KeyboardResult);
KeyboardResult = null;
if (useKeyboardResult)
{
return input;
}
}
return null;
}
And your Update method should contain something like this:
if (KeyboardInputRequested)
{
string result = GetKeyboardInput();
}
if (result != null)
{
//use result here
}
It is unlikely that the on-screen keyboard was packaged in the XNA DLLs for PC, but if you really want to find out, you could research a free .NET decompiling program (such as ILSpy) and look through it.
Also, you can't use the chatpad either. I would recommend either making your own on-screen keyboard that is usable by a controller, or maybe, if you can, using the MonoGame framework (an open-source version of XNA), and modifying it to have an on-screen keyboard on Windows.
You could also make a separate program that acts as a virtual keyboard controlled by the controller.

Categories