如何在Windows 7下记录来自非标准键的键事件
本文关键字:非标准 事件 记录 Windows 7下 | 更新日期: 2023-09-27 18:06:43
我有一个双重问题,这两个分支我都不熟悉。
背景:我从微星公司买了一台新的笔记本电脑。这款光驱没有安装机械弹出按钮,这是我以前从未见过的一个特点。相反,一个数字弹出按钮是功能键上方"智能条"的一部分。不幸的是,智能条背后的软件非常糟糕。即使在被动模式下,它也会占用屏幕空间和,它会占用任务栏的空间。我已经卸载了这个软件。
项目:我想写一个程序,在启动时运行,不显眼地躺在后台。该程序的唯一功能是使弹出按钮弹出CD驱动器。
第一步:我想知道当我按下那个按钮时会发生什么。我怀疑我不需要编写任何代码,但可以使用一个实用程序——我的一个同事提到了一个"事件记录器",但谷歌没有提供任何有用的东西。
第二步:我想写说的程序,这意味着我需要与WIN32或可能的。net框架进行交互,以执行相同的功能调用窗口使用当我右键单击cd驱动器下的"我的电脑",然后单击"弹出"。"
问题:第一个问题:您知道一个实用程序可以告诉我按下按钮时会发生什么吗?
第二个问题:你知道我需要调用那个事件的什么函数来弹出驱动器吗?
第三个问题:我是否忽略了一些明显的其他路径?我愿意接受任何答案,从"我有同样的问题,这是我写的解决方案的源代码"到"我想也许这个教程会帮助你?"
我更喜欢用C/c++/c#工作,但我愿意接受其他建议。正如标题所述,我在Windows 7下工作。
*对于好奇的人来说,这个软件是MSI S-Bar,出于某种原因,它被吹捧为这系列笔记本电脑的"功能"。
我自己也遇到过同样的问题。我不知道你是否熟悉自动热键,但是用它写了一个脚本来代替s-bar。
AutoHotKey (AHK)是一个面向Windows的开源宏创建和脚本语言,具有大量的功能。您可以通过程序本身运行脚本,也可以将它们编译成可在任何计算机上运行的可执行文件。只需几行代码就可以使弹出式CD驱动器工作:
;CD eject button
SC142::
Drive, Eject
return
SC142为键码。在每台MSI笔记本电脑上可能都不一样,但是如果你在脚本的顶部加上"#InstallKeybdHook"一行。您可以通过GUI查看AHK看到的所有键盘事件。
我能够将大多数按钮映射到我的MSI笔记本电脑上的各种功能,尽管AHK无法从其中一个捕获键盘事件。
例如:;Star key (AHK only receives KeyUp event)
SC139 UP::
Run, Control
return
;CinemaPro Key
SC13B::
Run, "C:'Program Files'Media Player Classic'mpc-hc64.exe"
return
热键
如果它们是键盘的一部分(只是因为它们看起来像,并不意味着它们真的是),它们将有一个扫描码。SetWindowsHookEx可以用来找出扫描码,以及对它做出反应-下面的代码应该有帮助:
class Program
{
static void Main(string[] args)
{
var myClass = new MyClass();
myClass.Install();
Application.Run(new Form()); // You need a form, not sure why.
myClass.Uninstall();
}
}
public class MyClass : CriticalFinalizerObject
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(HookType idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("winmm.dll")]
static extern Int32 mciSendString(String command, StringBuilder buffer, Int32 bufferSize, IntPtr hwndCallback);
private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
private enum HookType : int
{
WH_KEYBOARD = 2,
WH_KEYBOARD_LL = 13
}
private enum WindowsMessage : int
{
WM_KEYUP = 0x101
}
private HookProc _myCallbackDelegate;
private IntPtr _hook;
public MyClass()
{
_myCallbackDelegate = MyCallbackFunction;
}
public void Install()
{
Uninstall();
using (Process process = Process.GetCurrentProcess())
using (ProcessModule module = process.MainModule)
{
_hook = SetWindowsHookEx(HookType.WH_KEYBOARD_LL, _myCallbackDelegate, GetModuleHandle(module.ModuleName), 0);
}
}
public void Uninstall()
{
var ptr = Interlocked.Exchange(ref _hook, IntPtr.Zero);
if (ptr != IntPtr.Zero)
UnhookWindowsHookEx(ptr);
}
private IntPtr MyCallbackFunction(int code, IntPtr wParam, IntPtr lParam)
{
if (code >= 0 && wParam == (IntPtr)WindowsMessage.WM_KEYUP)
{
var sk = Marshal.ReadInt32(lParam);
// This can be used to find the scancode.
// Press the key and watch the console to find out the scancode.
Console.WriteLine("ScanCode: 0x{0:x4}", sk);
if (sk == 0x0041) // 0x0041 is A
{
// We can't hold up the hook for too long; start the
// tray open on another thread.
new Action(OpenTray).BeginInvoke(null, null);
}
}
return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
}
private void OpenTray()
{
mciSendString("set CDAudio door open", null, 0, IntPtr.Zero);
}
~MyClass()
{
Uninstall();
}
}
人机交互设备将更难与之交互。
正在弹出光驱。不需要admin权限
#include <windows.h>
#include <vfw.h>
#include <stdio.h>
#pragma comment(lib, "Vfw32.lib")
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
char msg[512];
HANDLE h;
DWORD bytesreturned;
h = CreateFile("''''.''cdrom0", MAXIMUM_ALLOWED, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (h == INVALID_HANDLE_VALUE)
{
sprintf(msg, "CreateFile: %u'n", GetLastError());
MessageBox(NULL, msg, "ejectcd", MB_OK);
return 1;
}
if (!DeviceIoControl(h, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &bytesreturned, NULL))
{
sprintf(msg, "DeviceIoControl: %u'n", GetLastError());
MessageBox(NULL, msg, "ejectcd", MB_OK);
return 1;
}
return 0;
}
check here:
c:CDR
c#:OpenCD
您必须使用mciSendString
API。看这里的SO链接,如果你要编程的话
您可以编写一个简单的程序,在按下弹出键时轮询键盘。使用GetKeyboardState()并检查被按下的是什么。或者你可以处理WM_KEYDOWN消息,并在你点击弹出键时检查键代码。
你试过在控制面板里找吗?进入控制面板>监控>键设置-从那里你可以重新分配键盘上的键。