Hi i have been tasked with creating a keyboard hook that works as follows. The user must press ctrl+shift+r anywhere(doesn't have to be in the form) and then the program must start recording the keys that the user is entering and write them to a text file(the key codes) and press ctrl+shift+r agian to stop recording. I can do this when the form is focused but i have no idea on how to write keyboard hook for this. I have been told to use the user32 dll but i have no idea how that works.

I have written this program to be able to work IF the form is focused but i now need this to work as a keyboard hook and not only if the Form is focused. I will provide code on something that i have tried but it only works to start recording but then it does not record the keys that i press after that.

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);
    }

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());
                    }
                }
            }
        }
    }

If anybody could pleas tell me how to use the user32 dll(import it and use it) or how to alter this code so that it works if you press any key outside of the form and the system can still write that key to the text file. Thx everyone

0 Answers