📄 keyboard.cs
字号:
using System;
using System.Runtime.InteropServices;
namespace PInvokeLibrary
{
/// <summary>
/// Summary description for Keyboard.
/// </summary>
public class Keyboard
{
private enum CheckMethod
{
kOS,
kRegistry
}
private static bool IsKeyboard(CheckMethod method)
{
if (method == CheckMethod.kRegistry)
{
uint dwordResult;
UIntPtr hkey = UIntPtr.Zero;
try
{
int result = Registry.RegOpenKeyEx(Registry.HKCU, "SOFTWARE\\Microsoft\\Shell", 0, Registry.KeyAccess.None, ref hkey);
if (Registry.ERROR_SUCCESS != result)
return false;
byte[] bytes = null;
uint length = 0;
Registry.KeyType keyType = Registry.KeyType.None;
result = Registry.RegQueryValueEx(hkey, "HasKeyboard", IntPtr.Zero, ref keyType,
null, ref length);
if (Registry.ERROR_SUCCESS != result)
return false;
bytes = new byte[Marshal.SizeOf(typeof(uint))];
length = (uint)bytes.Length;
keyType = Registry.KeyType.None;
result = Registry.RegQueryValueEx(hkey, "HasKeyboard", IntPtr.Zero, ref keyType,
bytes, ref length);
if (Registry.ERROR_SUCCESS != result)
return false;
dwordResult = BitConverter.ToUInt32(bytes, 0);
return (dwordResult == 1);
}
finally
{
if (UIntPtr.Zero != hkey)
{
Registry.RegCloseKey(hkey);
}
}
}
else
{
uint flags = KBDI_KEYBOARD_ENABLED | KBDI_KEYBOARD_PRESENT;
return ((GetKeyboardStatus() & flags) == flags);
}
}
/// <summary>
/// Indicates whether or not the system has keyboard hardware.
/// </summary>
public const uint KBDI_KEYBOARD_PRESENT = 0x0001;
/// <summary>
/// Indicates whether or not the keyboard hardware is enabled.
/// </summary>
public const uint KBDI_KEYBOARD_ENABLED = 0x0002;
/// <summary>
/// Indicates whether or not the keyboard hardware has ENTER and ESC keys.
/// </summary>
public const uint KBDI_KEYBOARD_ENTER_ESC = 0x0004;
/// <summary>
/// Indicates whether or not the keyboard hardware has alphanumeric keys.
/// </summary>
public const uint KBDI_KEYBOARD_ALPHA_NUM = 0x0008;
/// <summary>
/// This function returns the status of the hardware keyboard.
/// </summary>
/// <returns>This function returns a bit mask indicating whether or
/// not a keyboard is present and what its capabilities are.</returns>
[DllImport("coredll.dll")]
public static extern uint GetKeyboardStatus();
/// <summary>
/// Run a test of the Registry class.
/// </summary>
/// <param name="showLine">Delegate called to show debug information</param>
public static void TestProc(MainTest.DisplayLineDelegate showLine)
{
showLine("Checking registry...");
if (IsKeyboard(CheckMethod.kRegistry))
showLine("Keyboard detected");
else
showLine("No keyboard detected");
showLine("Checking OS...");
if (IsKeyboard(CheckMethod.kOS))
showLine("Keyboard detected");
else
showLine("No keyboard detected");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -