Results 1 to 11 of 11

Thread: [Resolved] Simulating keystokes and focus issues (virtual game pad for tablet UPC)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    11

    [Resolved] Simulating keystokes and focus issues (virtual game pad for tablet UPC)

    Edit: I finally found something that works! Hooking the mouse, preventing actual mouse clicks when the mouse is over the control form and calling the click events manually did the trick

    This is the situation.

    I'm working on a virtual game pad to be controlled by a mouse (or more likely a touch screen on a tablet UPC). I started working on it 2 days ago and it is nearing completion and works exactly as I had hoped except for one issue.

    I'm using the keybd_event API to simulate keystrokes on the target application. It does the job rather well, except for the fact that keybd_event requires focus on the target in order for it to work, and with the application I've used for testing (Project64 1.6) and assumedly other applications, the keys that are simulated are lost every time the target application loses focus, which happens quite a lot, in fact every time the virtual pad is clicked to press another button.

    This isn't an issue in most cases, however if a virtual button has been set to hold down (by clicking on it and dragging away), clicking anything else will make that key get released, at least until it resends all of the ones that are supposed to be held down. Even then it isn't always an issue, but for example if I'm using it to play Mario Kart and I want to hold down a banana behind me, as soon as I try to steer or anything else it will be let go.

    So what I'm asking is, does anybody have a possible solution to this problem? E.g. a way to send keys to an application that doesn't have focus (I've tried PostInput to each of the 4 classes in Project64 to no avail, though it was working in Notepad edit...may work with some outside insight), a way to prevent the keystrokes being lost when the target loses focus, a way to prevent the target form from ever losing focus, or to prevent the game pad form from getting focus, but still being able to click the controls on it (even if it means I have to get the status of the mouse with a timer and use its x/y coords and clicking status to create my own click event).

    Source attached
    I apoligize for the mess that the code and documentation is, I'm far from professional.
    Attached Files Attached Files
    Last edited by xXSnowyXx; Jan 18th, 2011 at 10:41 AM.

  2. #2

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    11

    Re: [VB6] Simulating keystokes and focus issues (virtual game pad for tablet UPC)

    My topic seems to have fallen off the bottem of the list without a single reply...I beleive a bump is in order (is that against the rules? I havn't been able to find it mentioned anywhere)

    I know its a tricky issue but someone must have an answer

  3. #3
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: [VB6] Simulating keystokes and focus issues (virtual game pad for tablet UPC)

    You might try subclassing your game pad window and intercepting the WM_ACTIVATE message, to prevent your window from getting focus. Unfortunately subclassing is delicate in VB6, especially in newer Windows versions.

    It's tough for me to figure out exactly what your issue is. I think you want to send a key down event for, say, the J key, and then you want to send a key down + key up event for, say, the A key. Sending the A key requires clicking the left arrow on your V-Pad form, which momentarily shifts focus to your app. You seem to be saying that when focus shifts, all keys currently held down are for some reason released, as far as Project64 is concerned. To be honest, this explanation seems unlikely to me.

    I tried your app. I held down the blue button (i.e. got a thick black border around it by clicking and dragging away from the button) and then clicked the left arrow. The result in a Notepad window is "jja". The first j comes from initiating the blue button hold. The next characters, ja, come from clicking the left arrow. Any extra left arrow clicks generate "ja". It seems to me the middle j shouldn't be there. That is, any extra left arrow clicks should only generate "a". When you "hold down" a button, I don't see why you wouldn't just stop at sending a key down event when the hold is initiated. Sending extra strokes when other buttons are pressed seems like a mistake.

    On an unrelated note, you might want to set the BorderStyle of your V-Pad to None and add an "X" of your own. If Project64 is full-screen, this should prevent you from noticing focus shifts.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    11

    Re: [VB6] Simulating keystokes and focus issues (virtual game pad for tablet UPC)

    The actual reason it sends it again is to to somewhat fix this very issue. If you blank out the resendkeys function it will behave normally, but in the apps I've tried (project64 and chocolate doom) the keystates will be lost when the form is clicked again, which is an issue difficult to determine in notepad.

    Like I said, it doesn't make much of a difference most of the time, eg just driving along and steering in Mario Kart isn't significantly affected by very short key releases. But it is still an issue, plus switching focus all the time creates about a 50ms lag before the keys can be sent which I don't like, and worse yet that varies depending on how fast your computer is (or can go with the current application that is running).

    And it already has an option for removing the frame, just tick the no frame box and it does exactly that but you'll have to click customize and move it to the appropriate position beforehand or itll get stuck centre-screen. if only noticing the focus shifts was the problem.

    I have no idea how to subclass though...its a term I've never heard before, but I will look into it

    Edit: tried to catch wm_activate and immediately set the target back to the foreground...it prevented the control form from getting focus (and receiving clicks), but the target form still lost focus long enough to release the keys (I tested it with the actual keyboard rather than simulating keystrokes to make sure). If I were able to set focus to the target when it lost focus, or perhaps cancel the focus change somehow, rather than setting it back to the target when the control form gets focus that might work, but thats something else entirely.
    Last edited by xXSnowyXx; Jan 8th, 2011 at 10:28 PM.

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: [VB6] Simulating keystokes and focus issues (virtual game pad for tablet UPC)

    Quote Originally Posted by xXSnowyXx View Post
    And it already has an option for removing the frame
    Whoops, I missed that. VB6 doesn't natively support DPI scaling, which I've set my installation of Windows to use, so the labels were all unreadable. Incidentally, if you plan on distributing this program in any way you'd want to look at DPI scaling issues, but it seems like this is a personal-use app which is why I didn't mention it originally.

    tried to catch wm_activate and immediately set the target back to the foreground...it prevented the control form from getting focus (and receiving clicks)
    Yes; as you said, you would have to simulate your own click event if your app isn't active.

    I'm not an expert on Windows messaging, but you might be able to subclass the external app's window (using it's hWnd instead of your own in the SetWindowLong call). You could then catch the WM_ACTIVATE message and not pass it along to the original event handler. That would theoretically prevent the app from knowing it had been deactivated and then reactivated. The only issue is that Windows might not allow you to intercept another app's message, or send it along to the app's routine.

    (With this setup you wouldn't subclass your own window, and so you wouldn't need to simulate a click event.)
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    11

    Re: [VB6] Simulating keystokes and focus issues (virtual game pad for tablet UPC)

    Yes, but unfortunately VB doesn't allow subclassing other applications (so it seems). I've seen some examples around that use a c++ dll in conjunction with a VB app to achieve the same effect, though my experience with c++ is limited so doing so may be difficult. I'll get it installed and try it nonetheless

    Edit: I tried this code as a proof of concept on my way to getting it to work with my app, and when I connect to notepad I can intercept the keydowns and mouseclicks, etc, but I can't intercept WM_ACTIVATE or WM_SETFOCUS...nothing shows up. The closest thing I can get is WM_PAINT, though with the main app (rather than the edit child) that only triggers when the form gets focus, if that. I tried watching what happens with spy++ and it could tell when those events fired...the dll must not be sending these events, though I'm unsure why this is. I'll keep trying, but anything insight would be helpful.
    Last edited by xXSnowyXx; Jan 12th, 2011 at 07:34 AM.

  7. #7
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: [VB6] Simulating keystokes and focus issues (virtual game pad for tablet UPC)

    I'm sorry, you're dealing with Windows subsystems I'm only passingly familiar with. A useful search phrase might be "hook WM_ACTIVATE", though that will give solutions in all languages. It's unclear to me why certain messages wouldn't get intercepted. Just in case, are you certain they're being sent to the emulator?
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    11

    Re: [VB6] Simulating keystokes and focus issues (virtual game pad for tablet UPC)

    Well spy++ seems to think they are.

    I'm also unfamiliar with the workings of windows subsystems...that and everything else I've tried to far in an attempt to fix this issue. If there is anyone here that has any idea what's going on, I'd love to hear from you

  9. #9
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: [VB6] Simulating keystokes and focus issues (virtual game pad for tablet UPC)

    Spy++ seeing the messages is actually very encouraging. Maybe the issue is just moeur's code for one reason or another. Have you been able to find a different implementation?
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  10. #10

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    11

    Re: [VB6] Simulating keystokes and focus issues (virtual game pad for tablet UPC)

    There was another one up here somewhere that I tried but I couldn't get it working...I suspect that it has something to do with the face that I HAVE NO IDEA WHAT I'M DOING
    ...ahem

    I looked into hooking getmessage (since it supposedly allows you to modify messages before they are activated) but I havn't found a working example for that yet. I also tried a shell hook that has an event when windows get activated, but it still has the same issue.
    ...is there nobody else here with any input?

  11. #11
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: [VB6] Simulating keystokes and focus issues (virtual game pad for tablet UPC)

    Quote Originally Posted by xXSnowyXx View Post
    ...is there nobody else here with any input?
    I'd bet a good chunk of people who would otherwise help are hanging out in the .NET forums nowadays. If you get really desperate you might try using .NET to do this instead; the Express editions are free, at least. It wouldn't surprise me at all if the documentation and available libraries for system hooks in .NET are much better than for VB6.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width