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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With