Page 1 of 1

How to press a key(Keyboard.Press) intstead of a click

Posted: Mon Nov 16, 2015 3:34 pm
by ejji09
Hi,

I want to press Ctrl+F2 key event in my application.

Can anyone suggest me that how to press the Ctrl+F2 event.

Thankyou.

Re: How to press a key(Keyboard.Press) intstead of a click

Posted: Mon Nov 16, 2015 3:52 pm
by lucian.teodorescu
Hi ejji09,

Did you tried Add New Action > Key Shortcut? Choose that and replace 'X' with Ctrl+F2.

Hope it helps?

Re: How to press a key(Keyboard.Press) intstead of a click

Posted: Mon Nov 16, 2015 3:57 pm
by ejji09
Am using code module.

I want to press the key event Ctrl+F2 in code module, I think you you gave me the answer for recording module.

Re: How to press a key(Keyboard.Press) intstead of a click

Posted: Mon Nov 16, 2015 4:03 pm
by odklizec
You did not specify you look for code in your initial post ;)

Anyway, do you know you could examine code of each recorded action and even convert recorded actions to UserCode? Just select the recording action and press Ctrl+Enter. This shortcut should redirect you to code behind the selected recording action. Another way is to right click the selected action and from the appeared context menu select View Code.

Re: How to press a key(Keyboard.Press) intstead of a click

Posted: Mon Nov 16, 2015 4:24 pm
by ejji09
Am using code module to write my test case and for the keypress event i used the syntax as

code:

Code: Select all

Keyboard.Press("{ControlKey down}{F2}");
and it worked fine, but after the completion of the test case i was unable to type anything, all the keys in keyborad are not functioning and when i press the control key then all keysare working again and then i was able to type.

Re: How to press a key(Keyboard.Press) intstead of a click

Posted: Mon Nov 16, 2015 4:33 pm
by odklizec
Yes, it's because you probably never called {ControlKey up} in your code? In other words, if you want to use Keyboard.Press like you did, you must always use {ControlKey down} {ControlKey up} sequence. Otherwise, the Ctrl key remains stuck in down position.

Or use the code behind the suggested Key Shortcut, which looks like this:

Code: Select all

Keyboard.Press(System.Windows.Forms.Keys.F2 | System.Windows.Forms.Keys.Control, Keyboard.DefaultScanCode, Keyboard.DefaultKeyPressTime, 1, true);