Simulating Shift Key

This is a discussion on Simulating Shift Key within the Windows Programming forums, part of the Platform Specific Boards category; Hi everyone, I'm trying to simulate keyboard keys. I can successfully do this using the keybd_event() or SendInput() functions, however, ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    11

    Thumbs down Simulating Shift Key

    Hi everyone,

    I'm trying to simulate keyboard keys. I can successfully do this
    using the keybd_event() or SendInput() functions, however, I've
    run into a little problem...

    I want to simulate the shift key, but not to capitalize letters etc.
    I need to simulate the shift key to select characters in an application. For example, if I have the word foo typed in notepad and the cursor is after the last 'o'. I want to simulate a shift press, then simulate 3 VK_LEFT presses and then release the shift. Here is some code:
    Code:
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event(VK_LEFT, 0, 0, 0);
    keybd_event(VK_LEFT, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_LEFT, 0, 0, 0);
    keybd_event(VK_LEFT, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_LEFT, 0, 0, 0);
    keybd_event(VK_LEFT, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    Now, when I use VK_SHIFT or VK_LSHIFT, the cursor will move left three times but will not select any characters. When I use VK_RSHIFT, my cursor will move left three times and select all three characters, but will not release the shift key, so I have to manually hit the shift key to release it.

    I've tried everything and have looked everywhere for information on this. Has anyone run into this problem?

    Thanks,
    Ryan Kegel

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Have you tried sending the window the EM_SETSEL message? That might be an easier way to select text.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    11
    I just tried it but it would not do anything, I know that I am getting the correct window too, since the GetWindowText() returns what window I want to select the text from... Other commands such as the WM_CLOSE work for this window, but not EM_SETSEL or WM_CUT etc...

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I have had trouble with keybd_event(). You are setting the second parameter to 0. I have found that this does not always work. Try setting the second parameter to the hardware scan code for the key. The last section of part 6 of my console tutorial has an example, and a utility program for finding out the scan code.

    http://www.adrianxw.dk/SoftwareSite/...Consoles6.html
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    11
    I've tried changing the second parameter to the scan code, but I find that this produces the same results as before.

    Code:
    keybd_event(VK_RSHIFT, MapVirtualKey(VK_RSHIFT, 0), 0, 0);
    keybd_event(VK_LEFT, MapVirtualKey(VK_LEFT, 0), 0, 0);
    keybd_event(VK_LEFT, MapVirtualKey(VK_LEFT, 0), KEYEVENTF_KEYUP, 0);
    keybd_event(VK_LEFT, MapVirtualKey(VK_LEFT, 0), 0, 0);
    keybd_event(VK_LEFT, MapVirtualKey(VK_LEFT, 0), KEYEVENTF_KEYUP, 0);
    keybd_event(VK_RSHIFT, MapVirtualKey(VK_RSHIFT, 0), KEYEVENTF_KEYUP, 0);
    I also tried
    Code:
    keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), 0, 0);
    keybd_event(VK_LEFT, MapVirtualKey(VK_LEFT, 0), 0, 0);
    keybd_event(VK_LEFT, MapVirtualKey(VK_LEFT, 0), KEYEVENTF_KEYUP, 0);
    keybd_event(VK_LEFT, MapVirtualKey(VK_LEFT, 0), 0, 0);
    keybd_event(VK_LEFT, MapVirtualKey(VK_LEFT, 0), KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), KEYEVENTF_KEYUP, 0);
    I looked at the On-Screen keyboard that comes with windowsXp, and it does what I'm trying to do. I wonder how they accomplish this...


  6. #6
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    im not to sure if this is right but it may be, once you have set the focus and you have highlited it with rshift, use this to release it

    Code:
    keybd_event(VK_RSHIFT, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
    i hope that helps, good luck
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    11
    I tried that... Still a no go.

    The interesting this is when I step through the code with the debugger. Once it signals the VK_SHIFT, i can move my cursor and it will select text, so I know the shift key is down. Then I step though the VK_LEFT's and they work too, then I process the VK_SHIFT up, and this turns off the selection. So in the debugging process this works great. But when I run the program and the focus is notepad or wordpad, it does not select the text.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    11
    Windows is very strange...

    It turns out, simulating the shift key to select text only works correctly when NUMLOCK is off...

    The heck?

    Oh well, problem solved. I'm giving myself 10 reputation points...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21