如何在 Java、C#、C++、Visual Basic、Delphi 等中显示、隐藏、移动或调整屏幕键盘的大小
您可以使用 Windows 消息来控制虚拟键盘。
示例:
WM_CSKEYBOARD = WM_USER + 192;
WM_CSKEYBOARDMOVE = WM_USER + 193;
WM_CSKEYBOARDRESIZE = WM_USER + 197;
// to show keyboard
PostMessage(FindWindow('TFirstForm', 'hvkFirstForm'), WM_CSKEYBOARD, 1, 0);
// to close keyboard
PostMessage(FindWindow('TFirstForm', 'hvkFirstForm'), WM_CSKEYBOARD, 2, 0);
// to fade keyboard
PostMessage(FindWindow('TFirstForm', 'hvkFirstForm'), WM_CSKEYBOARD, 3, 0);
// to toggle (show/hide) keyboard
PostMessage(FindWindow('TFirstForm', 'hvkFirstForm'), WM_CSKEYBOARD, 4, 0);
// to move keyboard (Left, Top - new position)
PostMessage(FindWindow('TFirstForm', 'hvkFirstForm'), WM_CSKEYBOARDMOVE, Left, Top);
// to resize keyboard
PostMessage(FindWindow('TFirstForm', 'hvkFirstForm'), WM_CSKEYBOARDRESIZE, Width, Height);
Visual Basic 2008 (VB.NET) 示例代码:
Const WM_CSKEYBOARD = &H400 + 192
Const WM_CSKEYBOARDMOVE = &H400 + 193
Const WM_CSKEYBOARDRESIZE = &H400 + 197
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
'Open/show Hot Virtual Keyboard in Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim hWnd As Integer
hWnd = FindWindow("TFirstForm", "hvkFirstForm")
PostMessage(hWnd, WM_CSKEYBOARD, 1, 0)
End Sub
'Close Hot Virtual Keyboard in Visual Basic
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim hWnd As Integer
hWnd = FindWindow("TFirstForm", "hvkFirstForm")
PostMessage(hWnd, WM_CSKEYBOARD, 2, 0)
End Sub
'Move Hot Virtual Keyboard in Visual Basic; Move it first then show it
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim hWnd As Integer
hWnd = FindWindow("TFirstForm", "hvkFirstForm")
PostMessage(hWnd, WM_CSKEYBOARDMOVE, 200, 200)
PostMessage(hWnd, WM_CSKEYBOARD, 1, 0)
End Sub
'Toggle Hot Virtual Keyboard in Visual Basic
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim hWnd As Integer
hWnd = FindWindow("TFirstForm", "hvkFirstForm")
PostMessage(hWnd, WM_CSKEYBOARD, 4, 0)
End Sub
'Fade Hot Virtual Keyboard in Visual Basic
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim hWnd As Integer
hWnd = FindWindow("TFirstForm", "hvkFirstForm")
PostMessage(hWnd, WM_CSKEYBOARD, 3, 0)
End Sub
'Change the keyboard type and show it
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
'Change the Registry entry for the required keyboard
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\ComfortSoftware\hvk", "KeyboardName", "Name of your chosen keyboard")
'Open the keyboard
Dim hWnd As Integer
hWnd = FindWindow("TFirstForm", "hvkFirstForm")
PostMessage(hWnd, WM_CSKEYBOARD, 1, 0)
End Sub
'Change to another keyboard type and show it
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
'Change the Registry entry for the required keyboard
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\ComfortSoftware\hvk", "KeyboardName", "Name of another chosen keyboard")
'Open the keyboard
Dim hWnd As Integer
hWnd = FindWindow("TFirstForm", "hvkFirstForm")
PostMessage(hWnd, WM_CSKEYBOARD, 1, 0)
End Sub
C# 示例代码:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public const Int32 WM_USER = 1024;
public const Int32 WM_CSKEYBOARD = WM_USER + 192;
public const Int32 WM_CSKEYBOARDMOVE = WM_USER + 193;
public const Int32 WM_CSKEYBOARDRESIZE = WM_USER + 197;
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern Int32 FindWindow(string _ClassName, string _WindowName);
[DllImport("User32.DLL")]
public static extern Boolean PostMessage(Int32 hWnd, Int32 Msg, Int32 wParam, Int32 lParam);
Int32 hWnd = FindWindow("TFirstForm", "hvkFirstForm");
PostMessage(hWnd, WM_CSKEYBOARD, 1, 0 ); // Show
PostMessage(hWnd, WM_CSKEYBOARD, 2, 0); // Hide
PostMessage(hWnd, WM_CSKEYBOARDMOVE, 0, 0); // Move to 0, 0
PostMessage(hWnd, WM_CSKEYBOARDRESIZE, 600, 300); // Resize to 600, 300
C++(CLR 语法)示例代码:
using namespace System::Runtime::InteropServices;
using namespace System::Security::Permissions;
using namespace Microsoft::Win32;
const System::UInt32 WM_USER = 1024;
const System::UInt32 WM_CSKEYBOARD = WM_USER + 192;
const System::UInt32 WM_CSKEYBOARDMOVE = WM_USER + 193;
[DllImport("user32.dll")]
extern IntPtr FindWindow(String^ lpClassName, String^ lpWindowName);
[DllImport("user32.dll")]
extern IntPtr PostMessage(System::IntPtr hWnd, System::UInt32 Msg, int wParam, int lParam);
[assembly:RegistryPermissionAttribute(SecurityAction::RequestMinimum, All = "HKEY_CURRENT_USER")];
....blah blah blah you normal code...
void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
// Open/show the Hot Virtual Keyboard
IntPtr hWnd;
hWnd = FindWindow("TFirstForm", "hvkFirstForm");
PostMessage(hWnd, WM_CSKEYBOARD, 1, 0);
}
void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
// close the Hot Virtual Keyboard
IntPtr hWnd;
hWnd = FindWindow("TFirstForm", "hvkFirstForm");
PostMessage(hWnd, WM_CSKEYBOARD, 2, 0);
}
void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
//Move the Hot Virtual Keyboard; Move it first then show it
IntPtr hWnd;
hWnd = FindWindow("TFirstForm", "hvkFirstForm");
PostMessage(hWnd, WM_CSKEYBOARDMOVE, 200, 200);
PostMessage(hWnd, WM_CSKEYBOARD, 1, 0);
}
void button4_Click(System::Object^ sender, System::EventArgs^ e)
{
//Toggle the Hot Virtual Keyboard
IntPtr hWnd;
hWnd = FindWindow("TFirstForm", "hvkFirstForm");
PostMessage(hWnd, WM_CSKEYBOARD, 4, 0);
}
void button5_Click(System::Object^ sender, System::EventArgs^ e)
{
//Fade the Hot Virtual Keyboard
IntPtr hWnd;
hWnd = FindWindow("TFirstForm", "hvkFirstForm");
PostMessage(hWnd, WM_CSKEYBOARD, 3, 0);
}
void button6_Click(System::Object^ sender, System::EventArgs^ e)
{
//Change the keyboard type and show it
System::Object ^kname="NumPad";
//Change the Registry entry for the required keyboard
RegistryKey ^key= Registry::CurrentUser->OpenSubKey ( "Software\\ComfortSoftware\\hvk",true);
key->SetValue("KeyboardName",kname);
//Open the keyboard
IntPtr hWnd;
hWnd = FindWindow("TFirstForm", "hvkFirstForm");
PostMessage(hWnd, WM_CSKEYBOARD, 1, 0);
}
Java 示例代码:
/*
* This file is heavily based on Jawin: http://jawinproject.sourceforge.net/
*
* assumes Hot Virtual Keyboard is loaded...
*
*/
package client.keyboard;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.jawin.COMException;
import org.jawin.FuncPtr;
import org.jawin.ReturnFlags;
import org.jawin.io.LittleEndianInputStream;
import org.jawin.io.LittleEndianOutputStream;
import org.jawin.io.NakedByteStream;
public class ComfortSoftwareKeyboard {
protected static final String COMFORT_SOFTWARE_WINDOW_NAME = "hvkFirstForm";
protected static final String COMFORT_SOFTWARE_CLASS_NAME = "TFirstForm";
protected static final int WM_USER = 1024;
protected static final int WM_CSKEYBOARD = WM_USER + 192;
protected static final int WM_CSKEYBOARDMOVE = WM_USER + 193;
protected static final Call FIND_WINDOW = new Call("USER32.DLL", "FindWindowW", "GG:I:", 8);
protected static final Call POST_MESSAGE = new Call("USER32.DLL", "PostMessageW", "IIII:I:", 16);
private static ComfortSoftwareKeyboard INSTANCE = new ComfortSoftwareKeyboard();
public static ComfortSoftwareKeyboard getInstance() {
return INSTANCE;
}
protected int getWindowHandle() throws COMException, IOException {
FuncPtr findWindow = null;
findWindow = new FuncPtr(FIND_WINDOW.getDllName(), FIND_WINDOW.getFunctionName());
NakedByteStream bs = new NakedByteStream();
LittleEndianOutputStream leo = new LittleEndianOutputStream(bs);
leo.writeStringUnicode(COMFORT_SOFTWARE_CLASS_NAME);
leo.writeStringUnicode(COMFORT_SOFTWARE_WINDOW_NAME);
byte[] b = findWindow.invoke(FIND_WINDOW.getParameterDescription(), FIND_WINDOW.getStackSize(), bs, null,
ReturnFlags.CHECK_FALSE);
LittleEndianInputStream leis = new LittleEndianInputStream(
new ByteArrayInputStream(b));
int l = leis.readInt();
findWindow.close();
return l;
}
public int move(int x, int y) throws COMException, IOException {
int hWnd = getWindowHandle();
FuncPtr postMessage = null;
postMessage = new FuncPtr(POST_MESSAGE.getDllName(), POST_MESSAGE.getFunctionName());
NakedByteStream bs = new NakedByteStream();
LittleEndianOutputStream leo = new LittleEndianOutputStream(bs);
leo.writeInt(hWnd);
leo.writeInt(WM_CSKEYBOARDMOVE);
leo.writeInt(x);
leo.writeInt(y);
byte[] b = postMessage.invoke(POST_MESSAGE.getParameterDescription(), POST_MESSAGE.getStackSize(), bs, null,
ReturnFlags.CHECK_FALSE);
LittleEndianInputStream leis = new LittleEndianInputStream(
new ByteArrayInputStream(b));
int l = leis.readInt();
postMessage.close();
return l;
}
public int setVisible(boolean visible) throws COMException, IOException {
int hWnd = getWindowHandle();
FuncPtr postMessage = null;
postMessage = new FuncPtr(POST_MESSAGE.getDllName(), POST_MESSAGE.getFunctionName());
NakedByteStream bs = new NakedByteStream();
LittleEndianOutputStream leo = new LittleEndianOutputStream(bs);
leo.writeInt(hWnd);
leo.writeInt(WM_CSKEYBOARD);
leo.writeInt(visible ? 1 : 2);
leo.writeInt(0);
byte[] b = postMessage.invoke(POST_MESSAGE.getParameterDescription(), POST_MESSAGE.getStackSize(), bs, null,
ReturnFlags.CHECK_FALSE);
LittleEndianInputStream leis = new LittleEndianInputStream(
new ByteArrayInputStream(b));
int l = leis.readInt();
postMessage.close();
return l;
}
public static void main(String[] args) throws Exception {
try {
ComfortSoftwareKeyboard keyboard = ComfortSoftwareKeyboard.getInstance();
keyboard.setVisible(true);
Thread.sleep(1000);
keyboard.setVisible(false);
Thread.sleep(1000);
keyboard.setVisible(true);
for (int i = 0; i < 100;i++) {
keyboard.move(i, i);
}
} catch (COMException e) {
} finally {
}
}
}
class Call {
private int stackSize;
private String functionName;
private String parameterDescription;
private String dllName;
public Call(String dllName, String functionName, String parameterDescription, int stackSize) {
this.stackSize = stackSize;
this.functionName = functionName;
this.parameterDescription = parameterDescription;
this.dllName = dllName;
}
public int getStackSize() {return stackSize;}
public String getFunctionName() {return functionName;}
public String getParameterDescription() {return parameterDescription;}
public String getDllName() {return dllName;}
}
如果您无法使用 Windows 消息,请下载并尝试这些文件:
https://hotvirtualkeyboard.com/files/ShowKB.exe
https://hotvirtualkeyboard.com/files/HideKB.exe
https://hotvirtualkeyboard.com/files/ToggleKB.exe
https://hotvirtualkeyboard.com/files/MoveTopKB.exe
https://hotvirtualkeyboard.com/files/MoveBottomKB.exe
https://hotvirtualkeyboard.com/files/MoveLeftKB.exe
https://hotvirtualkeyboard.com/files/MoveRightKB.exe
https://hotvirtualkeyboard.com/files/MoveKB.exe
(命令行格式: MoveKB.exe Left Top)
https://hotvirtualkeyboard.com/files/SetNameKB.exe
(命令行格式: SetNameKB.exe KeyboardName)
您可以使用 Virtual Keyboard Commander 来试用它的工作方式
如果您使用 HTML 编写自助服务终端软件,可以使用特殊的 JavaScript 函数来控制屏幕键盘。
使用 JavaScript,您可以显示、隐藏或移动键盘。只需使用特殊的 JavaScript 函数将与键盘相关的信息添加到浏览器标题中,应用程序就会监视标题的更改。
从这里下载包含 JavaScript 函数和示例的文件: https://hotvirtualkeyboard.com/commander.html
WM_CSKEYBOARDMOVE: 解决使用多个不同 DPI 的屏幕时移动键盘的问题。
例如,屏幕 1 的分辨率为 2560x1440,在 Windows 显示设置中设置为"125% 缩放"。屏幕 2 为 1920x1080,设置为"100% 缩放"。
打开 hvk.exe 文件的 Windows 应用程序兼容性设置,并将 DPI 行为设置为"应用程序",这意味着应用程序管理所有与 DPI 相关的计算。启用此设置后,您可以将物理像素值传递给 API 的移动函数,键盘窗口将精确移动到该位置。
如果您想以编程方式激活此兼容模式(例如,使用您自己的安装程序),则需要设置以下注册表项:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers:
"C:\Program Files\HotVirtualKeyboard\hvk.exe"="~ HIGHDPIAWARE"
这将应用于所有用户。或者,您可以将此项放入当前用户 (HKCU) 的注册表中。