AutoHotkey Community

It is currently May 25th, 2012, 5:36 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 52 posts ]  Go to page Previous  1, 2, 3, 4  Next
Author Message
 Post subject:
PostPosted: May 18th, 2007, 10:14 am 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
The problem with !ESC is that it produces side effects that appears to depend on environment in your script. May be hard od esay to workaround. I am losing my hear with FM problem easily...

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 18th, 2007, 10:31 am 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Funny, I tried to isolate the problem but in this little script I don't lose focus to the menu when I click tray. That is, I lose focus only sometimes...

I notice how to reproduce focus lost with the sample below.
Click the tray icon, click the submenu, click anywhere aside (not the menu) to close the menu, click the tray again, the focus is lost.

Little difference doesn't remove the focus
Click the tray icon, click the submenu, ESC, click the tray.

Code:
CoordMode, Mouse, Screen
#SingleInstance, force
   Menu, Tray, Add, Show Menu, TrayDefault
   Menu, Tray, Default, Show Menu
   Menu, Tray, Click, 1
   
   menu := MMenu_Create()
   menu2 := MMenu_Create()
   loop, 10 {
      MMenu_add(menu, "item" A_Index, "Shell32.dll:" A_Index)
      MMenu_add(menu2, "item" A_Index, "Shell32.dll:" A_Index+60)
   }
   MMenu_Add(menu, "menu2", "Shell32.dll:20", "", "m" menu2)
return

Handler:
return

TrayDefault:
   MouseGetPos, x, y
   MMenu_Show(menu, x, y, "Handler")
return


Download sample here (with MMenu)

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 18th, 2007, 10:36 am 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
When I add this to the code above code, Sean, menu is closed:

Code:
DetectHiddenWindows, on
...
TrayDefault:
   SetTimer, Activate, 500
   MouseGetPos, x, y
   MMenu_Show(menu, x, y, "Handler")
return

Activate:
   SetTimer, Activate, off
   WinActivate, ahk_class #32768
return


I wonder how come the same code works with Notepad

The complete code:

Code:
DetectHiddenWindows, on
CoordMode, Mouse, Screen
#SingleInstance, force
   Menu, Tray, Add, Show Menu, TrayDefault
   Menu, Tray, Default, Show Menu
   Menu, Tray, Click, 1
   
   menu := MMenu_Create()
   menu2 := MMenu_Create()
   loop, 10 {
      MMenu_add(menu, "item" A_Index, "Shell32.dll:" A_Index)
      MMenu_add(menu2, "item" A_Index, "Shell32.dll:" A_Index+60)
   }
   MMenu_Add(menu, "menu2", "Shell32.dll:20", "", "m" menu2)
return

Handler:
return

TrayDefault:
   SetTimer, Activate, 500
   MouseGetPos, x, y
   MMenu_Show(menu, x, y, "Handler")
return

Activate:
   SetTimer, Activate, off
   WinActivate, ahk_class #32768
return

#include includes
#include MMenu.ahk
#include structs.ahk



2Sean
In test I uploaded MMenu_Show is changed so it calls TrackPopUpMenu with 0x180 flag.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 18th, 2007, 11:11 am 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
More investgation:

MMenu stores handles to menus it created in array MMenu_aMenu[i] where i is i-th menu created.

so, I added this after creating menus:

Code:
hMenu :=  MMenu_aMenu[1] + 0
hMenu2 :=   MMenu_aMenu[2] + 0


and changed Activate func to:

Code:
Activate:

   SetTimer, Activate, off
   h := WinExist("ahk_class #32768")

   msgbox HMenu:%hMenu%`nHMenu2: %hMenu2%`n H:%h%
return


And I get H different then any of MMenu menu handles.

Then, this

Code:
Activate:

   SetTimer, Activate, off
   h := WinExist("A")
   WinGetClass, class, ahk_id %h%

   msgbox HMenu:%hMenu%`nHMenu2: %hMenu2%`n H:%h%, %class%
return

shows that when menu is visible, its parent is acctually active... (MMenu_hParent)

Then, this is strange. Shows 1 (proving my menu is the only windows with that class ATM )
Code:
Activate:

   SetTimer, Activate, off
   WinGet, out, list, ahk_class #32768
   msgbox %out%
   
return


I wonder why windows changes handle to the menu eachtime it is called for some small amount...


BTW, Menu losing focus is common problem around, not just in AHK. It has ben requested more then once and reported as bug, but this is defnitely not an AHK bug more like some weird WIn behavior...

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 18th, 2007, 1:20 pm 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
majkinetor wrote:
I notice how to reproduce focus lost with the sample below.
Click the tray icon, click the submenu, click anywhere aside (not the menu) to close the menu, click the tray again, the focus is lost.

In my test, it doesn't lose focus.

Quote:
When I add this to the code above code, Sean, menu is closed:

I think I found out why the menu is closed: AHK generates double ALT key presses. I noticed it through AHK's key history.
I don't know why AHK generates these ALT key presses atm.
Another mystery is why then these alt keys doesn't close the menu in notepad case.
Maybe timing issue? The Alt keys were generated before the menu got activated in notepad case?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 18th, 2007, 2:34 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Quote:
I think I found out why the menu is closed: AHK generates double ALT key presses. I noticed it through AHK's key history.
I don't know why AHK generates these ALT key presses atm.

I wonder why it generates ALT keys at all.... WinActivate does that. If you remove it, no keys are there.

This doesn't generate keys but doesn't work (also hides the menu):

Code:
Activate:
   SetTimer, Activate, off
   h := WinExist("ahk_class #32768")
   DllCall("SetForegroundWindow", "uint", h)
return

I tried also directly to use menu handle created by MMenu with h := MMenu_aMenu[1], it doesn't help but doesnt' close the screen.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 18th, 2007, 2:55 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Perhaps Chris can tell us why WinActivate generates to ALTs

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 18th, 2007, 3:10 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
I had an error above I used SetForegrounWin instead SetActiveWin...


Code:
Activate:
   SetTimer, Activate, off
   h := WinExist("ahk_class #32768")
   DllCall("SetActiveWindow", "uint", h)   
return


This kills the menu again, without having ALTs.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 18th, 2007, 3:33 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
You can get some interesting results with this setup.

1. Go to MMenu_Show function and make menu parent inactive
Gui 77:Show, x0 y0 w100 h100 noactivate

This will pin the menu on screen once you call it via tray so it doesn't dissapear.

2. Set this code in Activate:
Code:
Activate:
   SetTimer, Activate, off
   h := WinExist("ahk_class #32768")
   DllCall("SetActiveWindow", "uint", h)
   tooltip %h% %Menu_aMenu[1]%   ;current handle, creation menu handle
return


3. Open Winspector. Click the tray icon, menu is pinned and tooltip is shown. Drop the winspector (+) button on MMenu. Now when Winspector select it in the tree, RMB and say "Watch this windows class.." and watch its messages. After this, every time you run the script pop up with messasges will open in Winspector for that instance of class. So you can anaylise what is going on when activation message is sent.

Check out the winspector screen

Image

17th message (selected one) is the last one when menu shows. Other messages are received after timer executed. Notice the toooltip and the winspector title.

After this you can return activation in MMenu_Show and see how things behave realtime. I am just monitoring.

The strange thing I notice is that in both cases, when menu has and doesn't has a focus, I have the same number of messages (17), first 17 above. None of them is connected to activation

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 18th, 2007, 3:44 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
This made me think...

If I do

Quote:
Activate:
SetTimer, Activate, off
WINMOVE, ahk_class #32768, , 0, 0
return


I clearly got what I suppose to do.... WHAT THE HELL IS GOING ON.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 18th, 2007, 3:54 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
This is the Winspector Properties report for Notepade case and MMenu case:

Image

The only difference: WM_EX_LAYERED in MMenu (left is MMenu)


Found this at MSDN

To display a context menu for a notification icon, the current window must be the foreground window before the application calls TrackPopupMenu or TrackPopupMenuEx. Otherwise, the menu will not disappear when the user clicks outside of the menu or the window that created the menu (if it is visible). However, when the current window is the foreground window, the second time this menu is displayed, it displays and then immediately disappears. To correct this, you must force a task switch to the application that called TrackPopupMenu. This is done by posting a benign message to the window or thread, as shown in the following code sample:

SetForegroundWindow(hDlg);

// Display the menu
TrackPopupMenu( hSubMenu,
TPM_RIGHTBUTTON,
pt.x,
pt.y,
0,
hDlg,
NULL);

PostMessage(hDlg, WM_NULL, 0, 0);

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 18th, 2007, 11:02 pm 
Offline

Joined: April 21st, 2007, 9:16 pm
Posts: 178
Hum, this discussion is becoming too complex for me. Remember, I'm a beginner. I don't even know what is MMenu. Anyway, I want to keep my script as simple and light as possible.

I have tested again the !{ESC} trick, but I confirm that it doesn't work. Even the simple example I've posted in the last message of the previous page doesn't work with Always On Top windows.
It is easy to verify that. Open two windows. Make one of them Always On Top. Activate the normal window, and then the Always On Top one. Go to the script menu. The window identified as the active window after sending !{ESC} is the normal window. It should be the Always On Top one, as it was the last active window.

I have also tested the WM_ACTIVATEAPP trick, but no luck neither. I can't get it to retrieve the right window. But it's a good method to dynamically change the layout of the tray menu before showing it. Thanks for that.

_________________
r0lZ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 19th, 2007, 12:08 am 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
majkinetor wrote:
To display a context menu for a notification icon, the current window must be the foreground window before the application calls TrackPopupMenu or TrackPopupMenuEx. Otherwise, the menu will not disappear when the user clicks outside of the menu or the window that created the menu (if it is visible). However, when the current window is the foreground window, the second time this menu is displayed, it displays and then immediately disappears. To correct this, you must force a task switch to the application that called TrackPopupMenu. This is done by posting a benign message to the window or thread, as shown in the following code sample:

Ah, I think I once read it long time ago while I was reading about why WM_NULL is called benign message...

Anyway, WinActivate or alike the menu turned out to be not a good idea, as it relys on unsupported behavior (with TPM_NONOTIFY).
Then, better monitor the AHK's trayicon message and store the active window:

Code:
MsgID: 1028, wParam: 1028, lParam: 0x200 ~ 0x209 (WM_MOUSEMOVE ~ WM_MBUTTONDBLCLK)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 19th, 2007, 12:29 am 
Offline

Joined: April 21st, 2007, 9:16 pm
Posts: 178
Sean wrote:
Then, better monitor the AHK's trayicon message and store the active window:

Code:
MsgID: 1028, wParam: 1028, lParam: 0x200 ~ 0x209 (WM_MOUSEMOVE ~ WM_MBUTTONDBLCLK)
Good idea! I'll try it.

_________________
r0lZ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 19th, 2007, 11:28 am 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
2 r0lZ
!ESC works here with TOP windows. I tested with Favmenu which sets the folder in Open/Save after user invoked the menu with fav folders via tray. I opened Notepad Open/Save, made several windows on top and activated the OS. Favmenu set the folder correctly.

It seems that exact behavior depends on many factors which are script dependent.

BTW, MMenu is replacement for AHK Menu command. I made it, U have it in S&F forum with docs. I used it here as I can tweak internals of the Menu API to see what is going on when menu is activated/selected etc. U are confused with right. I had short PM about this with Sean and we continued here. Also this is unrelated to your problem, but in Favmenu, those 2 problems are connected. The problem is that MMenu sometimes loses focus - not just MMenu, but any Menu implementation (AHK Menu, 3th party menus etc...). This happens in FM3 when I do !ESC, hense connectivity. Without !ESC MMenu doesn't lose focus. THere were several questions related to "Menus losing keyboard focus" in previous year.

Sean wrote:
Then, better monitor the AHK's trayicon message and store the active window:

Well... we tried everything else I think.... I will wait for r0lz to see what is going on with this now. Thx for idea.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 52 posts ]  Go to page Previous  1, 2, 3, 4  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: alanofoz, Cerberus, Google [Bot], poserpro, Sambo, tommy, Yahoo [Bot] and 22 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group