12

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.

Lion
  • 18,729
  • 22
  • 80
  • 110
Naresh
  • 2,667
  • 13
  • 44
  • 69
  • We need more information about the `Robot`-class to help you out on this one. Is it from another library or do you have the source for that one as well? Do you know what the code is intended to accomplish? – Karl-Johan Sjögren Jul 10 '12 at 09:10
  • 1
    Yesterday, similar topic was discussed. I hope this will help you, too: http://stackoverflow.com/questions/11402643/sendkey-send-not-working/11403269#11403269 – Alex Butenko Jul 10 '12 at 09:13
  • 1
    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. – Crollster Jul 10 '12 at 09:15
  • 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. But they are not working as per the expectation. Somebody suggested in his blog to execute these steps to make those API's working. – Naresh Jul 10 '12 at 10:15
  • I am also trying to understand what exactly the code does. But I am not aware of these Java API's. But I couldn't get much info on these key events VK_WINDOWS and VK_M. – Naresh Jul 10 '12 at 10:18
  • Just referencing a related post with related answer: https://social.msdn.microsoft.com/Forums/en-US/a4ff3770-c384-4526-9099-cf134be4dfcc/javaawtrobot-equivalent-in-net?forum=netfxbcl – David Mar 29 '18 at 17:53

3 Answers3

8

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.

William
  • 413
  • 9
  • 21
Osama Javed
  • 1,432
  • 1
  • 16
  • 21
3

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

tan js
  • 204
  • 2
  • 10
0

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.

Felinis
  • 47
  • 3