Capture keystrokes even if pressed outside the app

Asked

Viewed 80 times

-2

good morning

I did a little program where it shows the typed key, it’s pretty simple , just so I can learn how to use Windows form application. My goal was that when running another app , for example the word , it appeared on the keys that were being typed , but when opening another window it stops to capture. Obs: it’s my first/second time doing a post , pardon if it got mischievous.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace prj1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.TopMost = true;
        }

        private void button1_KeyPress(object sender, KeyPressEventArgs e)
        {
            lbl1.Text = e.KeyChar.ToString();
        }

        private void bnt1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

    
  • There are security issues involved in this case... What you describe is a Keylogger... why you want to capture typing out of your application?

  • Because a friend of mine does live on Witch , and he broke the web cam , and not to get an open space , he wanted something that showed what he is typing and clicking the mouse , I found programs on the internet that do it quietly , but wanted to learn

1 answer

0

Vi here a code you can try.

Using the dll User32 combined with a Timer in your Form, you can check the keys that are being pressed.

I do not know if it would be the best way to be used, but I tested and worked very well.

    public Form1()
    {
        InitializeComponent();
        timer1.Start();
    }

    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    public static extern Int16 GetAsyncKeyState(int vKey);

    private void timer1_Tick(object sender, EventArgs e)
    {
        while (true)
        {
            for (int key = 0; key < 255; key++)
            {
                //caso seja diferente da tecla pressionada, irá retornar zero
                if (GetAsyncKeyState(key) == -32767)
                {
                    char keyPress = Convert.ToChar(key);
                }
            }
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.