Is there a way to accept keystrokes through c# in a console app? Basically I want to apply macros/the f-keys to my program, thanks.

Recommended Answers

All 7 Replies

Yes, you need to subscribe your form to a key event.

Something like:

this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.KeyUpEvent);

public void KeyUpEvent(object sender, KeyEventArgs e){

//Code here to handle specific keys
}

Can alternative use KeyDown or KeyPress events instead of Keyup.

It's not a form though, it's a console app, any way to do it through a console app?

Ahh i misread that. You can just use Console.readline() and process the input then.

Ok but won't that require the user to type in "f9" or whatever then hit enter? I want to bypass that like you would in a form.

I've never used it, but console.readkey() might be what you are looking for.

commented: bumping your rep since the OP has zero rep power +1

After doing some research on using console.readkey(), it turns out this is exactly what I needed! Thank god .net 1.1 isn't the standard anymore as things like this were very limited back then. Thanks again!

Excellent! Glad I could help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.