Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Need some advice for sending CTRL in global mousehook


  • Please log in to reply
3 replies to this topic
Max
  • Guests
  • Last active:
  • Joined: --
Hi all!

I've been asking this in german forum and got advice to try it here. Hope this post fits within rules here, only connection to AHK is me asking how it handles such problems deep inside:)

so: i've registered a global mouse-hook (WH_MOUSE). i capture some events in firefox/mozilla and at a certain point i'd like to close current tab. i'd like to send a CTRL-w to do so (i guess that's an language independent shortcut).

problem with most windows applications is that they don't care for sent CTRL/SHIFT/... messages at all - they just use GetKeyboardState (or similar) - which is for sure easier (and results in no problems if such messages are missed).

so i changed hook to not send CTRL message, but use SetKeyboardState, but no luck. figured out that thats no way to go, since i'm within the same thread (which i'm blocking) that polls messsages queue. and before i return from hook i've got to reset CTRL to old value.

next step is to send a message to my exe to send messages right after i return from hook. but how to? SetKeyboardState works for same thread only - i'm in another.

Could AttachThreadInput help?
Some other ideas?
How does AHK solve this?

I guess i could write a firefox/mozilla plugin, but i'd like to avoid this.

Thanks for any thoughts about this,
Max

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Hi, I'm not sure what you're aiming to do but you might find Hotkey, Send, and IfWinActive useful to your criterion.

Example:
^w::
IfWinActive, Mozilla
Send, {key combos to close tab, or whatever}
Return

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004

Could AttachThreadInput help [with SetKeyboardState]?

I have a vague impression that it would only help on Windows 9x.

next step is to send a message to my exe to send messages right after i return from hook. but how to? SetKeyboardState works for same thread only - i'm in another.

How does AHK solve this?

AutoHotkey's ControlSend command sends modifier keystrokes in two ways. The first way is a more forceful keybd_event(), which tends to interfere with the any typing the user might be doing in the foreground window. The other way is is a less intrusive method that is activated by explicitly specifying "up" and "down" events as in this example:

ControlSend, Edit1, {Alt down}f{Alt up}, Untitled - Notepad

Internally, the program does both of the following to send modifier keystrokes to the target window:

1) AttachThreadInput + SetKeyboardState(): These might only be effective on Win9x but it's been a long time since I tested it.

2) The following code, in which aVK is the virtual key code and aSC is the scan code:
// lowest 16 bits: repeat count: always 1 for up events, probably 1 for down in our case.
// highest order bits: 11000000 (0xC0) for keyup, usually 00000000 (0x00) for keydown.
LPARAM lParam = (LPARAM)(aSC << 16);
if (aEventType != KEYUP)  // i.e. always do it for KEYDOWNANDUP
	PostMessage(aTargetWindow, WM_KEYDOWN, aVK, lParam | 0x00000001);
if (aEventType != KEYDOWN)  // i.e. always do it for KEYDOWNANDUP
	PostMessage(aTargetWindow, WM_KEYUP, aVK, lParam | 0xC0000001);


  • Guests
  • Last active:
  • Joined: --

I have a vague impression that it would only help on Windows 9x.

1) AttachThreadInput + SetKeyboardState(): These might only be effective on Win9x but it's been a long time since I tested it.


i've been changing a bit here and there, now it works fine. AttachThreadInput & SetKeyboardState seem to work fine in win2000 & xp.

what i'm doing now: i've got a exe that loads hook dll, i hook WH_MOUSE & WH_KEYBOARD. instead of doing things right inside hook (which would never work if you like to send SHIFT/CTRL) i use a pipe to communicate with thread in exe. here i process messages and send them to window where needed. GetKeyboardState to save old state, AttachThreadInput, SetKeyboardState for SHIFT or CTRL, Send/PostMessage, Sleep (needed so foreign application can process message before i return), AttachThreadInput (to detach) and SetKeyboardState to old state.

works like a charm and seems to be only way to send CTRL/SHIFT "enhanced" messages for win95-xp.

Thanks a lot for your detailed answer.