- Home /
On Screen Keyboard (PC and Console) best practices
I'm looking at implementing text input for my game. Currently it's a PC, Mac and Linux (Steam) game, but we want to fully support controller input as well (Big Picture Mode, as well as porting to consoles in the future).
There are a number of occurrences where we'll want users to input text for names (utilizing Steam Workshop with in game designs primarily).
I'm wondering if there's a best practice for on screen keyboards for this, or if it's something most people program out themselves with their UI? It's a frustrating thing to search for (using Keyboard as a key word doesn't really bring anything useful up, and the things you do find are primarily for mobile).
It seems like most systems have a built in Keyboard, so I'd hope to just use those. But I'm fairly uncertain the best way to utilize them (and as stated earlier, it's quite difficult to search for).
Thanks in advance for any help/tips, it's greatly appreciated.
I don't think the built-in keyboard is available for desktop platforms in Unity... In that case, you'd have to write your own, which definitely isn't a trivial task if you're supporting more than one language.
Yeah, that's been on my $$anonymous$$d since talks with our publisher about localization for 'Simplified Chinese, German, French, Russian, Spanish, and Japanese' as suggested languages (they're handling the translations).
I was hoping there were some functions or something else I was missing.
Answer by Vagonn · Jan 28, 2016 at 04:25 PM
First you need to create VirtualKeyboard.cs script
using UnityEngine;
using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class VirtualKeyboard
{
[DllImport("user32")]
static extern IntPtr FindWindow(String sClassName, String sAppName);
[DllImport("user32")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
private static Process _onScreenKeyboardProcess = null;
/// <summary>
/// Show the touch keyboard (tabtip.exe).
/// </summary>
public void ShowTouchKeyboard()
{
ExternalCall("C:\\Program Files\\Common Files\\Microsoft Shared\\ink\\tabtip.exe", null, false);
//ExternalCall("TABTIP", null, false);
}
/// <summary>
/// Hide the touch keyboard (tabtip.exe).
/// </summary>
public void HideTouchKeyboard()
{
uint WM_SYSCOMMAND = 274;
int SC_CLOSE = 61536;
IntPtr ptr = FindWindow("IPTip_Main_Window", null);
PostMessage(ptr, WM_SYSCOMMAND, SC_CLOSE, 0);
}
/// <summary>
/// Show the on screen keyboard (osk.exe).
/// </summary>
public void ShowOnScreenKeyboard()
{
//ExternalCall("C:\\Windows\\system32\\osk.exe", null, false);
if (_onScreenKeyboardProcess == null || _onScreenKeyboardProcess.HasExited)
_onScreenKeyboardProcess = ExternalCall("OSK", null, false);
}
/// <summary>
/// Hide the on screen keyboard (osk.exe).
/// </summary>
public void HideOnScreenKeyboard()
{
if (_onScreenKeyboardProcess != null && !_onScreenKeyboardProcess.HasExited)
_onScreenKeyboardProcess.Kill();
}
/// <summary>
/// Set size and location of the OSK.exe keyboard, via registry changes. Messy, but only known method.
/// </summary>
/// <param name='rect'>
/// Rect.
/// </param>
public void RepositionOnScreenKeyboard(Rect rect)
{
ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowLeft /t REG_DWORD /d " + (int)rect.x + " /f", true);
ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowTop /t REG_DWORD /d " + (int)rect.y + " /f", true);
ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowWidth /t REG_DWORD /d " + (int)rect.width + " /f", true);
ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowHeight /t REG_DWORD /d " + (int)rect.height + " /f", true);
}
private static Process ExternalCall(string filename, string arguments, bool hideWindow)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = filename;
startInfo.Arguments = arguments;
// if just command, we don't want to see the console displayed
if (hideWindow)
{
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
}
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
return process;
}
}
then call from your script like below
VirtualKeyboard vk = new VirtualKeyboard();
public void OpenKeyboard()
{
{
vk.ShowTouchKeyboard();
}
}
public void CloseKeyboard()
{
{
vk.HideTouchKeyboard();
}
}
I love you, @Vagonn ! If I were able to, I'd gladly bear your children. Seriously, thank you!!!
Hello @Vagonn , first of all . Thank you for this. An answered prayer for me. Would like to ask if is there any way to reposition the keyboard?
Thanks
Has there been an updated version of the code where the keyboard can be re positioned? Just tried the function now and it doesn't seem to be doing anything.
Answer by alex17pat · Sep 05, 2017 at 11:02 AM
One of the Best On Screen Virtual Keyboards which I know are
Free Virtual Keyboard
On-Screen Keyboard Portable
Comfort On-Screen Keyboard Lite
Microsoft On-Screen Keyboard
Click-N-Type
source:- http://merabheja.com/free-on-screen-virtual-keyboards/
Answer by kulger · Oct 21, 2016 at 12:16 PM
hello, i tried it's working only under windows 8.
am stucked with window 10 , keyboared never show , even when you click on Tabtip.exe
but when you call it from execute panel "osk" it 's working , am confused .. and i need some help with windows 10,
yes, on windows 10 keyboard never show, only worked for win8
hello @vagonn , please can you show me the steps , i had tried but i didn't get any result ,i am confuse please i need some help
I know it is a late response, but @Vagonn shared here a really nice script.
The reason, that someone could have problems with showing the keyboard is that:
vk.ShowTouch$$anonymous$$eyboard();
Touch $$anonymous$$eyboard will work, and show, only on devices with touch input enabled. If You want to show it on standard windows, then You have to call:
Show/Hide OnScreen$$anonymous$$eyboard
methods from the script, for example:
vk.ShowOnScreen$$anonymous$$eyboard();
,
and if is not positioned not, set it's rect using:
RepositionOnScreen$$anonymous$$eyboard(Rect rect)
,
with example:
vk.RepositionOnScreen$$anonymous$$eyboard(new Rect(0, 0, 500, 200)
hi @Drakonno i followed your steps but still nothing is showing i really don't know why it did not work maybe the version of the unity 5.6 i am using please can you show me your step thanks in advance for your help
Your answer
Follow this Question
Related Questions
Removing Joystick Controls 2 Answers
Text Prompt Problem 1 Answer
Activate GUI Button with Enter Key 1 Answer
GUI buttons with alternate activation condition 1 Answer
OpenVR controllers Vive 2 Answers