在表格不可见时捕获键盘上的任何脂肪族?

capture any aliphatic on keyboard while form is not visible?

我试图从 A…Z 中捕捉任何字符,但我没有得到它 我使用全局热键,但他们只是操纵 F2…..F12 之类的东西 我该怎么做?
我试过了:

 public Form1()
    {
        InitializeComponent();
        // register the event that is fired after the key press.
        hook.KeyPressed +=
        new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
        // register the control + alt + F12 combination as hot key.
        hook.RegisterHotKey(GlobalHotKeys.ModifierKeys.Control | GlobalHotKeys.ModifierKeys.Alt, Keys.F12);
        hook.RegisterHotKey(GlobalHotKeys.ModifierKeys.Control | GlobalHotKeys.ModifierKeys.Alt, Keys.F2);

    }
    void hook_KeyPressed(object sender, KeyPressedEventArgs e)
    {
        // show the keys pressed in a label.
        MessageBox.Show(e.Key.ToString());

    }

我相信你应该使用 Global Keyboard Hook 从非活动或隐藏的形式捕获按键事件,你可以使用 Keyboard Hooks Library 这大大简化了过程,这里是一个基于提到的库的示例:

using System.Windows.Forms;
using MouseKeyboardActivityMonitor.WinApi;

namespace Demo
{
    public partial class MainForm : Form
    {
        private readonly KeyboardHookListener hook=new KeyboardHookListener(new GlobalHooker());

        public MainForm()
        {
            InitializeComponent();

            hook.KeyDown += hook_KeyDown;
            hook.Enabled = true;
        }

        void hook_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Control && e.Alt && e.KeyCode==Keys.F12)
                MessageBox.Show(@"Alt+Ctrl+F12 Pressed.");
        }
    }
}

控制台示例:

注意:添加 System.Windows.Forms.dll 作为对控制台应用程序的引用

using System;
using System.Threading;
using System.Windows.Forms;
using MouseKeyboardActivityMonitor.WinApi;

namespace Demo
{
    internal class KeyboardHook : IDisposable
    {
        private readonly KeyboardHookListener _hook = new KeyboardHookListener(new     GlobalHooker());

        public KeyboardHook()
        {
            _hook.KeyDown += hook_KeyDown;
            _hook.Enabled = true;
        }

        private void hook_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.Alt && e.KeyCode == Keys.F12)
                MessageBox.Show(@"Alt+Ctrl+F12 Pressed.");

        }

        public void Dispose()
        {
            _hook.Enabled = false;
            _hook.Dispose();
        }
    }

    internal static class Program
    {
        private static void Main()
        {
            var t = new Thread(() =>
            {
                using (new KeyboardHook())
                    Application.Run();
            });

            t.Start();

            Console.WriteLine(@"press 'C' key to exit application...");
            while (Console.ReadKey().Key != ConsoleKey.C){}

            Application.Exit();
        }
    }
}