Naresh
Naresh

Reputation: 2673

Java Robot class eqivalent code in C#

Can somebody help me in converting the following java code to C#.

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_WINDOWS);
robot.keyPress(KeyEvent.VK_M);
robot.keyRelease(KeyEvent.VK_WINDOWS);
robot.keyRelease(KeyEvent.VK_M);

I understood we have to use 'user32.dll'. But I am not sure which methods we have to call.

Upvotes: 12

Views: 24989

Answers (3)

tan js
tan js

Reputation: 294

InputSimulator is an excellent option in C# - NuGet it to load in the project.

Working Example in VS Studio 2019: In an authentication popup, to input text into username textbox having focus (cursor), which is not detected by browser dev-tools/ inspect element to automate using selenium:

InputSimulator sim = new InputSimulator(); 
// enter username: QAUser01 
sim.Keyboard.TextEntry("QAUser01"); 
// press Tab key 
sim.Keyboard.KeyPress(VirtualKeyCode.TAB); 
// Enter Password 
sim.Keyboard.TextEntry("acb@123"); 
// submit enter 
sim.Keyboard.KeyPress(VirtualKeyCode.RETURN);

more can be referred here: C# equivalent to Java Robot class

Thanks

Upvotes: 3

Osama Javed
Osama Javed

Reputation: 1442

If you are trying to simulate keyboard key presses, the following article should help you: http://www.codeproject.com/Articles/28064/Global-Mouse-and-Keyboard-Library

It has examples so it shouldn't be too hard to understand.

Upvotes: 8

Felinis
Felinis

Reputation: 47

The JAVA Robot class is designed for automated testing and operates at or below the HAL layer (Hardware Abstraction Layer). Simply generating keys and mouse movements programmatically is not the same thing as putting keys into the hardware keyboard buffer or the hardware mouse circuitry.

Upvotes: 0

Related Questions