使用键盘挂钩,而无需将窗体集中在C#上

时间:2019-05-14 16:12:12

标签: c# visual-studio keyboard-hook

我想编写一个使用键钩从键盘读取输入并将其写入文本文件的程序。

我需要创建一个键盘钩,例如,当我按Shift + ctrl + R时,它需要开始记录我在键盘上键入的内容并将这些键代码写入文本文件。

如果表单当前处于焦点状态,我知道如何将键盘上按下的键写入文本文件,但是如果表单不处于焦点状态,我还需要程序也能工作。

   public partial class Form1 : Form
{
    KeyboardHook hook = new KeyboardHook();
    public string pathName;
    public bool recording = false;
    int counter = 0;

    public Form1()
    {
        InitializeComponent();
        hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
        hook.RegisterHotKey(Opdrag_4C_ITRW316.ModifierKeys.Control | Opdrag_4C_ITRW316.ModifierKeys.Shift, Keys.R);
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        switch (keyData)
        {
            case Keys.Up:
                Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y - 10);
                break;
            case Keys.Right:
                Cursor.Position = new Point(Cursor.Position.X + 10, Cursor.Position.Y);
                break;
            case Keys.Down:
                Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y + 10);
                break;
            case Keys.Left:
                Cursor.Position = new Point(Cursor.Position.X - 10, Cursor.Position.Y);
                break;
            default:
                break;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

    private void btnFileSelect_Click(object sender, EventArgs e)
    {
        OpenfileChooser();
        Focus();  
    }

    public void OpenfileChooser()
    {
        OpenFileDialog fdg = new OpenFileDialog();
        fdg.ShowDialog();
        pathName = fdg.FileName;
        MessageBox.Show(pathName);
    }

    private void Hotkey_KeyDown(object sender, KeyEventArgs e)
    {

    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(pathName))
        {
            using (Stream str = new FileStream(pathName, FileMode.Create, FileAccess.ReadWrite))
            { }
        }
        else
        {
            MessageBox.Show("No file has been selected.");
        }
    }

    public void hook_KeyPressed(object sender, KeyPressedEventArgs e)
    {
        counter++;

        if (counter % 2 == 1)
        {
            lblRecording.ForeColor = Color.Red;
            lblRecording.Text = "Recording";
            recording = true;
            Focus();
        }
        else
        {
            lblRecording.ForeColor = Color.Blue;
            lblRecording.Text = "Not Recording";
            recording = false;
        }

        this.Focus();
    }

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (recording)
        {
            while (String.IsNullOrEmpty(pathName))
                OpenfileChooser();

            using (Stream str = new FileStream(pathName, FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(str))
                {
                    if (!(e.KeyCode == Keys.Control || e.KeyCode == Keys.Shift))
                    {
                        writer.Write(e.KeyCode.ToString().ToLower());
                    }                        
                    else if (e.Shift && e.KeyCode != Keys.HanjaMode)
                    {
                        if (e.KeyCode != Keys.Shift)
                            writer.Write(e.KeyCode.ToString().ToUpper());
                    }
                }
            }
        }
    }
}

我有以下代码起作用,因此当用户按下CTRL + Shift + R时,程序会说它开始记录,但是如果我键入,则键不会写入文本文件。我不确定我是否也需要将它们注册为钩子,但这可能会花费很多精力和时间。关于如何获取程序来记录我按下钩子CTRL + SHIFT + R之后所按下的键的任何想法,而不必专注于表格。

0 个答案:

没有答案