Kaozty
Kaozty

Reputation: 1

VB.Net, keep shift pressed, and release after certain function is done?

Is that possible?

I want to keep my shift pressed in an external application (such as fire fox) and release it as I pressed it again, I want it working just like the caps button.

to make it easier to understand, maybe something like

shift is pressed Keys.Shift.hold = true

and

shift is pressed again Keys.shift.hold = false

Upvotes: 0

Views: 345

Answers (1)

cf-
cf-

Reputation: 8866

Apologies in advance for the pseudocode; I don't know VB.net. If you're comfortable with C++, you can adapt code from this question, which I used as a reference.

You can use the SendInput Windows API. According to the function's description, it

Synthesizes keystrokes, mouse motions, and button clicks.

You will need to ensure your target program has a lower process integrity than yours - in other words, if your process isn't running as admin, don't try to feed events to an admin-level process - but since you mentioned Firefox I don't expect this to be an issue for you.

You'll want to set up a KEYBDINPUT structure like this:

wVk = VK_SHIFT
dwFlags = 0 (this will press a key. You'll want KEYEVENTF_KEYUP instead for releasing it.)

Then, set up an INPUT structure like this:

type = INPUT_KEYBOARD
ki = yourKeybdInputStruct

Now pass that whole INPUT structure to SendInput:

SendInput(1, &yourInputStruct, sizeof(yourInputStruct))

Alternately, you could try using the VB.net SendKeys class. There are some caveats that come with this class; most notably,

If your application is intended for international use with a variety of keyboards, the use of Send could yield unpredictable results and should be avoided.

Additionally, the official documentation says nothing about using a modifier key (Shift, Ctrl, Alt) by itself, nor does it appear to support holding a key down in the manner you describe, just sending a regular keystroke (keydown-keyup).

However, if you're willing to deal with those issues, this might work:

SendKeys.Send("+")

Upvotes: 1

Related Questions