Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Robot class eqivalent code in C#

Tags:

java

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.

like image 522
Naresh Avatar asked Jul 10 '12 09:07

Naresh


People also ask

Can we use Robot class in C#?

The Robot class is part of the standard JDK - it's meant to allow you to programmatically move the mouse, press buttons, etc - simulating user activity. As part of automation we are using Selenium. I need to maximize the browser before running a test case. There are some API's in selenium to maximize the window.

What is correct for Robot class mouseMove ()?

mouseMove(int x, int y) : move the mouse to a specified location of screen. keyPress(int k) : presses a given key with a specified keycode. keyRelease(int k) : releases a given key with a specified keycode.


1 Answers

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

like image 135
tan js Avatar answered Oct 12 '22 22:10

tan js