WH_KEYBOARD_LL hook only in my process main thread - winapi

Is it possible to have a WH_KEYBOARD_LL hook that only calls the hook function when my application/thread has the focus? Currently I'm receiving calls even when the application is not active.

Sure, the 4th argument to SetWindowsHookEx() can be a thread ID to make it selective. Pass the one for your UI thread, get it by calling GetCurrentThreadId().
Do note that this is not normally very useful, you can intercept keyboard messages in your message loop just as easily. Every GUI class library supports this, required to implement shortcut keystrokes. Even the winapi has this, TranslateAccelerator(). Strongly recommended, debugging a hook is very painful since a breakpoint in the hook callback or any function called by your callback will cause the keyboard to seize up for 5 seconds and your hook to be destroyed.

There's no way for you to install a hook and also apply some form of filter to suppress it firing in certain states. Once it is installed, it will fire.
So, either do nothing in your hook function when your application is inactive, or remove the hook when it becomes inactive. Or, do away with the hook altogether, and respond to the messages that arrive in your message queue.

Related

Stop event propagation in Meteor

I've hooked up some basic click events using Meteor's event maps.
How do I stop the event from propagating after being handled?
Meteor's docs state that
Right now, the event handlers are wired up with jQuery, and the event
object you receive is a jQuery event object.
However, neither return false nor e.stopPropagation() seem to work.
Currently, stopPropagation works in a limited set of circumstances. For propagation to stop between two handlers, the handlers must be specified on different templates, and there has to be an intervening DOM node container between the inner and outer template. A fix is under development right now and will go into an upcoming release.
Can you post a gist of your code so I can make sure your particular case will be addresed?
As a workaround, you might try e.stopImmediatePropagation(), which is supplied by jQuery and should keep any other handlers from running.
I ran across this question while researching this myself. The documentation for Blaze Event Maps is here.
For this problem in meteor you need to consider 2 functions depending upon what you want:
stopPropagation()
Prevent the event from propagating (bubbling) up to other elements. Other event handlers matching the same element are still fired, in this and other event maps.
stopImmediatePropagation()
Prevent all additional event handlers from being run on this event, including other handlers in this event map, handlers reached by bubbling, and handlers in other event maps.
What I wanted was to stop bubbling and stop propagating to other Meteor event handlers. So event.stopImmediatePropagation() did the trick and it is not really a workaround as advised above.

Handling windows button during Async BeginGetResponse in wp7

is there any way to handle the Windows button keypress within the *.Xaml.cs especially when the app is busy getting a request processed using an Asynchronous BeginGetResponse. is there a override handler like OnBackKeyPress?
What's the appropriate way to handle this use case? i'm already handling Application_Activated and deactivated in the App.Xaml.cs file for tombstoning.
You can't stop this from happening. When this happens the current page will get its OnNavigatedFrom override called so you could clean up your page and save state from this method.
Bear in mind that it won't be possible to tell whether this is due to the hardware Start key or if the user just navigated away by say pressing the Back key or tapping a button.
Update:
If you're trying to avoid a crash due to Fast App Switching interrupting your networking call you should rather handle this when you return to the application. Your WebRequest will be cancelled and you should handle this case as shown in this MSDN blog post.

How to show a Carbon dialog as part of a plugin for an application with an existing event loop?

I'm writing a plugin for an application and need to use Carbon to show a dialog. I have everything set up including the event handler, but I cannot possibly call RunApplicationEventLoop() because this would stall the host application.
How can I fix this? Will I need to create a separate thread and call RunApplicationEventLoop() from there?
-Joe
What makes you think you need to call RunApplicationEventLoop? The host app is presumably running an event loop, probably either using RunApplicationEventLoop or NSApplicationMain. By the way, would your dialog be modal? Modal is easier.

Handling CGEventTaps in an NPAPI plugin

I'm trying to create an NPAPI plugin to listen to the Media Keys on a macbook and pass that to javascript to control things like pandora or soundcloud. I'm using Spotify's SPMediaKeyTap library, which just wraps CGEventTap running on a separate thread.
My problem is that I use npn_invoke to call back to javascript. This works normally, but when it's triggered from the CGEventTap callback, it crashes the plugin. I realize this needs to be run from the plugin thread, and I've tried to pass it back to the main thread both by using [NSObject performSelectorOnMainThread] and [NSObject performSelector:onThread] with the thread i've stored away in the main plugin threads create method. Both of these solutions still crash on any npn call. Is there anything else that goes on when handling a CGEventTap event that causes state to be invalid for NPN browser interaction calls?
Don't try to second guess the threading model by saving the thread like you are; just use performSelectorOnMainThread to call NPN methods. I do this all the time and it works fine, so I'm guessing that something with your method of cross-thread marshalling isn't working the way it needs to.

Altering Qt Application keyboard events

I'm trying to do the following - given an keypress event in an QT Application, I want to intercept it, modify it (for instance, replacing Qt::Key_Up with 0x81001310), and send it into the app again.
Ideas?
Does overriding the event method work? If you do get all of the keypresses there, just consume the events you wish to replace and send new events that you want.
Might not work, just an idea that should be easy to test.
Be careful to not cause infinite recursions or loops :)
Edit:
If this doesn't work, you can always create an eventFilter and modify the events that way. However, if you do this, you might have to install the event filter for many things.

Resources