Is it okay to continuously hook and unhook keyboard? - vb.net

I have an issue with WindowsHookEx in vb.net. If my pc is overloaded especially from 3D rendering, windows automatically disconnects my keyboard hook and my hotkeys stop working. I searched around and it seems that there is no way to detect whether a hook is active or disconnected. So I tried this method presented by "moodforaday"
Is it possible to detect when a low-level keyboard hook has been automatically disconnected by Windows?
hook-has-been-automatically-d
He states that using GetLastInputInfo periodically and store GetLastInputInfo to another variable when a key is used and compare the results. If the tick is much newer than your older variable then its likely that its disconnected. Its a great method but the ticks can go up from other things like the mouse. In my Hook class there is no Mouse hook therefore I cannot store a variable of the tick count when the mouse is moved. So now I ended up having it create a new instance of the hook class and hook again. It checks every second if the stored tick is older than new tick by 10000 ticks.
Is it alright to keep creating new instances of Hooks? It will keep Hooking/Unhooking constantly and I'm wondering if that is going to be a problem for Windows.
Also if anyone has another method to detect if a hook is disconnected please let me know would fix this whole hassle.

Do your 3D rendering in a background thread. Use Control.Invoke only for code where you directly access UI controls.
Alternately, you could split the rendering into very small pieces and post them to yourself as messages, to be handled on the main thread. This way you will be able to handle both internal and external messages.
In both cases, your application will be responding in a timely fashion, Windows will have no reason to consider it non-responding, and your keyboard shortcuts will stay in place.

Related

Cocoa global hotkey consume key event

I've been trying for hours to use global hotkeys and "consume" the key event so it is not forwarded any more to the application where the key event is originally coming from.
So what I want to do is:
- a user presses a shortcut with application A in front, e.g. Cmd+F3
- my application (application B) receives this shortcut through the global event handler and sends mouse and keyboard events to application A
It's probably easiest to think of it as a macro.
I'm using DDHotkey and it works quite fine. The problem I have is that DDHotkey doesn't "consume" the key events and modifiers. That means that when my application starts sending mouse and keyboard events, the Cmd key from the actual global shortcut is still pressed.
This leads to erroneous behavior in my case (I'm double-clicking a textfield programmatically and that doesn't open when Cmd is pressed for example).
So what I'd like to do is really consume the key event and the modifier keys so that they are not forwarded to application A. Alternatively, I would "flush" the event queue before sending the key events to application A.
Is there any way to achieve this easily?
An even more reliable approach if it works for your use case would likely be to not script the UI by triggering mouse events and key presses, and instead use the Accessibility API to trigger the more high-level actions (like using Accessibility to tell a button it has been pressed). Unless the app contains some unfortunate code, that should not look at the modifier keys.
Telling the OS from an event monitor to remove key states would probably cause lots of issues: It would be confusing if the user then actually released the physical keys and a second keyUp came in. Even if the OS tries to avoid that, it is just asking for other edge case bugs - what if the user pressed a modifier key while your code is scripting the UI?
But if the applications you are scripting do not support Accessibility, nor AppleScript, nor any other more high level approach to automation, what you could do is wait for the user to release your hotkey (i.e. wait for keyUp events) and only then trigger your scripted actions. Might be necessary to use performSelector:withObject:afterDelay:0.0 to get out of the keyUp handler before you do that.

Program to respond to Keycodes while minimized

I need a program to respond while not active/not selected by user or minimized to KeyCodes.
Anyone got ideas? In VB.NET.
This won't work out of the box as key messages are only sent to the active window. A minimized window is never active.
What you could try is register system-wide hotkeys. You could also try to install a keyboard hook, however, this would affect the entire system and your application would receive all the keystrokes performed. This would require efficient filtering.

how to click on WebArea from QTP ?

In My test I want to click on object of Type WebArea which opens a webelement popup includes some fields that i need to test.
the problem that the popup not open after I click on WebArea object through the code.
the code I use as below.
Browser("WW").page("assessment").WebArea("areaassessment").Click
nothing hapens after the above line excuted.
Look into the HTML of the WebArea and see what action is triggering the popup. Normally it has something like onclick='showPopup();', but in other cases it is onmousedown or onmouseup.
If this is the case, you have to setup QTP accordingly. There are multiple roads to walk here, one is to see how you advanced web settings are configured. Go to Tools>Options>Web>Advanced and look in the Run Settings.
Setting the Replay Type to Event will replay your scripts by events (by default mousedown, mouseup and then mouseclick) or by mouse (You'll see your mouse pointer moving in this mode, QTP will replay by sending WM_* messages through the Windows api for movement to the correct screenlocation and triggering the click).
Allthough it replays a bit faster, if Run only click is checked, it is better to uncheck this to trigger all events / messages.
Events can also be fired by the FireEvent method:
Browser("WW").page("assessment").WebArea("areaassessment").FireEvent("onclick")
or through the object native methods:
call Browser("WW").page("assessment").WebArea("areaassessment").Object.click()
call Browser("WW").page("assessment").WebArea("areaassessment").Object.FireEvent("onclick")
As #AutomateChaos said there is probably an event that QTP isn't simulating, one way to work around this is to do as #AutomateChaos suggests and simulate the needed event. A simpler way is to change to device replay (as I described here and here).

How to emulate mouse clicking/mouse moving/key typing in the global context?

Emulate means to invoke these events programatically.
Global context means that these event invocations should affect the whole desktop (sort of global environment) rather than the application which produces them. Moreover, the application itself should have no windows - it has to simply execute in background and produce these events due to some logic. In other words, if, for example, this application puts mouse in "global" arbitrary position and invokes a double click event and there is an icon of some other application under the cursor then this "other" application should start.
Which library can I use to achieve it?
Note: I don't specify OS since I hope that the library is supposed to do it in a cross-platform way. If that's not possible then I will be fine with the Windows only solution.
I found out that Java's java.awt.Robot has all requested features.

ITfTextInputProcessor::Deactivate gets called unexpectedly on regaining focus

I am implementing a text service on windows. Things work fine. However when I shift window focus to another application and shift focus back to the original application, the selected text services gets de-activated (I notice a call to ITfTextInputProcessor::Deactivate). I think this call is unexpected. Post this call, The service has to be re-activated manually. I am surely doing something goofy. Just that I don't know what it is.
Offhand, I'd say that you are indeed doing something goofy. :) In particular, I'd pay careful attention to your ITfThreadMgrEventSink::OnSetFocus implementation (and, obviously, you need to implement ITfThreadMgrEventSink in your text service and connect it via AdviseSink if you haven't already.)
After more research, I've figured out what’s happening:
When you set focus back to Word, TSF gets the current thread’s active keyboard layout (actually a locale ID).
It then compares that keyboard layout with the language ID of the currently active text service.
If they’re different, TSF then activates the text service for the active keyboard layout, and deactivates any previously active text service.
I believe this behavior is different on Vista/Windows 7.
The fix would be to use LoadKeyboardLayout/ActivateKeyboardLayout to set the process keyboard layout in your ITfTextInputProcessor::Activate implementation. Apparently some apps also need you to call ITfInputProcessorProfiles::ChangeCurrentLanguage() as well.