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.
Vagonn
January 28, 2016, 4:25pm
2
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\ abtip.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();
}
}
kulger
October 21, 2016, 12:16pm
3
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,
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/