5

I have this bit of code which uses SendInput to send a key press but it doesn't work for when I want to long hold a key (eg long hold 'a' would return 'aaaaaaaaa' in notepad).

Now I have looked all over google and the only way I can see to get around this is to keep sending the input if I want a long hold. I don't want to do that as this will just simulate 'a' being pressed over and over again.

keyboard.wVk = 0;
keyboard.wScan = MapVirtualKey(key, 0);

keyboard.dwFlags = KEYEVENTF_SCANCODE;

if (index_vector_no)
    pressed[index_vector_no] = true;

keyboard.dwExtraInfo = 0;

input.type = INPUT_KEYBOARD;
input.ki = keyboard;

SendInput(1, &input, sizeof (input));

So I would like some answers to the following questions:

A) Am I right in thinking there is no way around this using SendInput and why doesn't it work for long hold?

B) What is an alternative method for successfully being able to send key down and key up signals. Preferably sending the keys to windows and not just to a particular application.

C) Is there a good lightweight C++ library I can use that handles global keyboard and mouse simulation?

Thanks in advance! =)

EDIT: Take a look at this post for more details of my problem: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_20833788.html

Ollie
  • 473
  • 3
  • 7
  • 19
  • Is this for Windows? Are you using VC++? – George Skoptsov May 05 '12 at 21:22
  • What's your problem with simulating it being pressed multiple times? The only reason I can think of that would need the difference would be...a window. – chris May 05 '12 at 21:27
  • yes it is on windows. and i'm just using normal C++ with .cpp file endings. i'm using the MinGW compiler if it makes a difference! @Chris this will be for controlling a game and so it will end up with the guy jerkerly moving forwards Thanks =) – Ollie May 05 '12 at 21:46
  • Set a state variable on `WM_KEYDOWN` and clear it on `WM_KEYUP`. Or is it not your game? – chris May 05 '12 at 21:51
  • @chris it's not my game, if it was it wouldn't be a problem! and before anyone is says anything it's to make my PSP a controller not so I can create a hack! :P – Ollie May 05 '12 at 21:57
  • I guess you could just `SendInput` the down and not the up until you need it. Not sure why I didn't think of that. – chris May 05 '12 at 21:58
  • @chris that is what I have done, it just doesn't work for long hold. Take a look at this: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_20833788.html – Ollie May 05 '12 at 22:04

1 Answers1

5

Repeating keystrokes is a feature of the keyboard controller, not of Windows or SendInput. You can certainly emulate it with a timer, repeatedly calling SendInput().

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • hmmm... how can windows put a limit on how fast a character is repeated? Does it just ignore some of the inputs the keyboard sends it? – Ollie May 05 '12 at 23:27
  • Windows can change the keyboard controller configuration, it is a two-way connection. Most obvious from the Keyboard applet in Control Panel. Irrelevant when you don't actually want to use a keyboard and fake input. – Hans Passant May 05 '12 at 23:32