Foreground Window Script

Discussion in 'General' started by trpling, Jan 19, 2018.

  1. trpling

    trpling Orc Centurion

    Messages:
    54
    If you are like me and don't use wineq it makes it a pain to alt tab and find the window you want. I wrote a small little python script that will bring the window by name to the front.
    Since I name my windows my char names this works for me but might not work for you. Either way here it is enjoy.

    Code:
    import win32gui, win32con
    import re
    import sys
    
    def RaiseWindowName(nameRe):
        cb = lambda x,y : y.append(x)
        wins = []
        win32gui.EnumWindows(cb, wins)
        tgtWin = -1
        for win in wins:
            txt = win32gui.GetWindowText(win)
            if re.match(nameRe,txt):
                tgtWin = win
                break
            
        if tgtWin >= 0:
            if win32gui.IsIconic(tgtWin) is 1:
                win32gui.ShowWindow(tgtWin, win32con.SW_RESTORE)
            win32gui.SetForegroundWindow(tgtWin)
    
    RaiseWindowName(sys.argv[1])
    
     
  2. Hive

    Hive Froglok Krup Watcher

    Messages:
    340
    Very cool!
    I use HotKeyNet to switch between my windows and it works pretty well. A basic hotkey would be

    Code:
    <hotkey Divide>
    <SendPC Local>
       <SendFocusWin>
       <If WinExists Morbo>
          <TargetWin Morbo>
          <SetForegroundWin>
       <If WinDoesNotExist Morbo>
             <TargetForegroundWin>
             <Key Divide>
             <SetForegroundWin>
    it uses the Divide key on the numpad to switch to Morbo, if I don't have Morbo logged on then it just uses the Divide key as normal. If you wanted to use this format all you would have to do is change "Divide" to whatever key you wish to use to switch to your window, and switch "Morbo" to whatever your EQ window is called. Note that when you have this script running no matter what you are doing if you have the designated EQ window running then the key you choose will always switch to that EQ window so try and use a key you would normally not use.
     
    • Agree Agree x 1
  3. trpling

    trpling Orc Centurion

    Messages:
    54
    I mainly did it because you can pass arguments to the script so lets say you had Morboheals you can just ./foreground.py Morboheals and it will switch. I am also thinking of just making it quicker and having a compiled program where it lists all the EQ windows and you can press a number and it switches but that might be V2.
     
  4. mackal

    mackal Pyrilen Fireblade

    Messages:
    2,267
    I was working on an autohotkey version of this, I can find/finish it if anyone is interested
     
  5. Hive

    Hive Froglok Krup Watcher

    Messages:
    340
    I really only ever need to switch to a character to loot something, and only if the corpse they are looting has more than 1 no drop item on it and I only want them to loot the one. So let's say I kill a PoFire mini and it drops plate and chain arms. Obviously I don't want whatever toon is getting the plate to also loot the chain, so the first character I have to switch to in order to loot the correct mold/pattern. The second character is easy because it's just /tar, /loot, /lootall. I'm pretty sure there is a way to loot only a certain slot of a corpse? That would take away a lot of switching for me.
     
  6. trpling

    trpling Orc Centurion

    Messages:
    54
    I think this is what you are looking for. I added it to my e3 with some other stuff i'm working on github.

    Code:
    #EVENT lootItem "<#1#> Loot Item #2# #3# #4# #5#"
    #EVENT lootItem "<#1#> Loot Item #2# #3# #4#"
    |pass charname corpseID and itemID and it will move to a corpse to loot the item you gave it
    SUB EVENT_lootItem(line, ChatSender, corpseID, charName, itemName, equipSlot)
       |only the person whose name we passed will do this
       /declare full_item_name local
       /if (!${Me.CleanName.Equal[${charName}]}) /RETURN
          |check the distance
       /if (!${Bool[${Spawn[corpseID].Distance.Int} < 100]}) { /RETURN
          |move to the corpse
          /target ID ${corpseID}
          /delay 7
          /squelch /stick 10
          /delay 5s
          /squelch /stick off
          /click rightclick target
          /delay 1s
          |check if window open
          /if (${Corpse.Open}) {
             /if (${Me.FreeInventory}) {
                |Gets the full item name
                /varset full_item_name ${Corpse.Item[${itemName}]}
                /if (${Corpse.Item[${itemName}].Lore} && ${FindItemCount[${Corpse.Item[${itemName}]}]}) {
                   /docommand ${ChatToggle} Item is Lore ${Corpse.Item[${itemName}]}
                   /return
                } else {
                   /itemnotify ${Corpse.Item[${full_item_name}].InvSlot} leftmouseup
                   /delay 5
                   |used for debug
                   /if (${Corpse.Item[${itemName}].NoDrop}) {
                      /notify ConfirmationDialogBox CD_Yes_Button leftmouseup
                      /delay 15
                      /docommand ${ChatToggle} Looting ${full_item_name}
                   }
                   /notify LootWnd LW_DoneButton leftmouseup
                   /delay 5
                   /if (${Select[${equipSlot},charm,leftear,head,face,rightear,neck,shoulder,arms,back,leftwrist,rightwrist,ranged,hands,mainhand,offhand,leftfinger,rightfinger,chest,legs,feet,waist,powersource,ammo]}) {
                      /autoinventory
                      /delay 2s
                      /call SwapItem "${full_item_name}" ${equipSlot}
                      /docommand ${ChatToggle} Equiping ${full_item_name} to ${equipSlot}
                   } else {
                      /delay 3
                      /autoinventory
                      /RETURN
                   }
                }
             }
    
          } else {
             /docommand ${ChatToggle} Corpse is not open will not loot
          }
       
       } else {
          /docommand ${ChatToggle} Corpse is too far get closer
       }
     
    /RETURN
     
    • Winner Winner x 1
  7. Hive

    Hive Froglok Krup Watcher

    Messages:
    340
    Duuuude...
    So usage would be?
    /bc Loot Item charname CorpseID ItemID
     
  8. trpling

    trpling Orc Centurion

    Messages:
    54
    /bc Loot Item Corpseid charname ItemName (if there are 2 items with some of the same name use something unique to the item like muramite head muramite sleeves use /bc Loot Item corpseID Morbo head.
    I made an alias for /lootitem ${target.ID} so i just target something pass the charname and item I want them to loot.
     
  9. Hive

    Hive Froglok Krup Watcher

    Messages:
    340
    The loot macro is brilliant btw. Been using it and it's worked very well. Thank you!
     
  10. dabs

    dabs Orc Pawn

    Messages:
    10
    Code:
    <hotkey NumpadInsert>
        <TargetWin "PEQ Warrior">
        <SetForegroundWin>
        <SendFocusWin>
       
    <hotkey NumpadEnd>
        <TargetWin "PEQ Cleric">
        <SetForegroundWin>
        <SendFocusWin>
       
    <hotkey NumpadLeft>
        <TargetWin "PEQ Wizard">
        <SetForegroundWin>
        <SendFocusWin>
    
    <hotkey NumpadHome>
        <TargetWin "PEQ Shaman">
        <SetForegroundWin>
        <SendFocusWin>
       
    <hotkey NumpadDown>
        <TargetWin "PEQ Bard">
        <SetForegroundWin>
        <SendFocusWin>
       
    <hotkey NumpadUp>
        <TargetWin "PEQ Berzerker">
        <SetForegroundWin>
        <SendFocusWin>
    
       
       
       
    <hotkey Numpad0>
        <TargetWin "PEQ Ranger">
        <SetForegroundWin>
        <SendFocusWin>
       
    <hotkey Numpad1>
        <TargetWin "PEQ 2Cleric">
        <SetForegroundWin>
        <SendFocusWin>
       
    <hotkey Numpad4>
        <TargetWin "PEQ Enchanter">
        <SetForegroundWin>
        <SendFocusWin>
    
    <hotkey Numpad7>
        <TargetWin "PEQ Druid">
        <SetForegroundWin>
        <SendFocusWin>
       
    <hotkey Numpad2>
        <TargetWin "PEQ 2Bard">
        <SetForegroundWin>
        <SendFocusWin>
       
    <hotkey Numpad8>
        <TargetWin "PEQ Beastlord">
        <SetForegroundWin>
        <SendFocusWin>
    
    
    
    
    <hotkey Left>
        <TargetWin "PEQ 1Mag">
        <SetForegroundWin>
        <SendFocusWin>
       
    <hotkey Down>
        <TargetWin "PEQ 2Mag">
        <SetForegroundWin>
        <SendFocusWin>
       
    <hotkey Right>
        <TargetWin "PEQ 3Mag">
        <SetForegroundWin>
        <SendFocusWin>
    
    <hotkey Up>
        <TargetWin "PEQ 4Mag">
        <SetForegroundWin>
        <SendFocusWin>
       
    <hotkey End>
        <TargetWin "PEQ 2Druid">
        <SetForegroundWin>
        <SendFocusWin>
       
    <hotkey Home>
        <TargetWin "PEQ 3Bard">
        <SetForegroundWin>
        <SendFocusWin>
    
    I use winEQ to name the windows

    Then my numpad is the hotokey switcher... press numlock to activate group 2... and use the arrow keys + home/end for group 3

    I just bought a gpad because i want group 4 and no more hotkeys lol




    Anyways if you're not using wineq here's my old c++ program that I used to use for eqmac, it will find every eqgame.exe and then rename them to unique window handler and then assign hotkeys to each window.

    It only assigns up to 6 atm because thats all the hotkeys i needed at the time but its easy to change

    Code:
    #include <stdio.h>
    #include <tchar.h>
    #include <windows.h>
    #include <winuser.h>
    #include <string>
    #include <sstream>
    
    
    void SetForegroundWindowInternal(HWND hWnd)
    {
     if(!::IsWindow(hWnd)) return;
    
     BYTE keyState[256] = {0};
     //to unlock SetForegroundWindow we need to imitate Alt pressing
     if(::GetKeyboardState((LPBYTE)&keyState))
     {
      if(!(keyState[VK_MENU] & 0x80))
      {
       ::keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
      }
     }
    
     ::SetForegroundWindow(hWnd);
    
     if(::GetKeyboardState((LPBYTE)&keyState))
     {
      if(!(keyState[VK_MENU] & 0x80))
      {
       ::keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
      }
     }
    }
    
    
    void EnableDebugPriv( )
     {
         HANDLE hToken;
         LUID sedebugnameValue;
         TOKEN_PRIVILEGES tkp;
         OpenProcessToken( GetCurrentProcess( ), TOKEN_ADJUST_PRIVILEGES |TOKEN_QUERY, &hToken );
         LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &sedebugnameValue );
         tkp.PrivilegeCount = 1;tkp.Privileges[0].Luid = sedebugnameValue;
         tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
         AdjustTokenPrivileges( hToken, false, &tkp, sizeof( tkp ), NULL, NULL );
         CloseHandle( hToken );
     }
    
    
    int main()
    {      
     //Find each EQ Window and set to appropriate name
     //1. First ask how many EQ windows will be open
     DWORD pId;
     int x = 0;
     int numOfWindows = 0;
     printf("Please enter # of EQ instances");
     scanf ("%d",&numOfWindows);
     EnableDebugPriv();
     
     //Loop through each instance and set all but first new names
     //x<numOfWindows - 1 so you always leave 1 with original name
     for(x=0; x<numOfWindows-1; x++)
     {
      HWND hWindow = FindWindow(NULL,"EQW beta 2.32");
      if(!hWindow)
      {
       printf("EQ window not found\n");
       system( "pause" );
       return 0;
      }
      else
      {
         GetWindowThreadProcessId(hWindow, &pId);
         HANDLE hOpen = OpenProcess( PROCESS_ALL_ACCESS, false, pId );
         if(!hOpen)
         {
          printf("Cannot open process.");
          system( "pause" );
          return 1;
         }
         else
         {
          std::string name = "EQWindow";
          std::ostringstream convert;
          convert << x+1;
          name += convert.str();
          char *charName = new char[name.size()+1];
          charName[name.size()] = 0;
          memcpy(charName,name.c_str(),name.size());
          SetWindowText(hWindow, charName);
         }
      }
     }
     
     
     
     enum{ZERO_KEYID = 0, ONE_KEYID = 1, TWO_KEYID = 2, THREE_KEYID = 3, FOUR_KEYID = 4, FIVE_KEYID = 5};
     RegisterHotKey(0, ZERO_KEYID, 0, 0x60); // register NumPad0 key as hotkey
     RegisterHotKey(0, ONE_KEYID, 0, 0x61); // register NumPad1 key as hotkey
     RegisterHotKey(0, TWO_KEYID, 0, 0x64); // register NumPad4 key as hotkey
     RegisterHotKey(0, THREE_KEYID, 0, 0x67); // register NumPad4 key as hotkey
     RegisterHotKey(0, FOUR_KEYID, 0, 0x62); // register NumPad2 key as hotkey
     RegisterHotKey(0, FIVE_KEYID, 0, 0x68); // register NumPad8 key as hotkey
     MSG msg;
           
     HWND eqWindow0 = FindWindow(NULL, "EQW beta 2.32");//Change window title name here
     HWND eqWindow1 = FindWindow(NULL, "EQWindow1");//example: "Untitled - Notepad"
     HWND eqWindow2 = FindWindow(NULL, "EQWindow2");
     HWND eqWindow3 = FindWindow(NULL, "EQWindow3");
     HWND eqWindow4 = FindWindow(NULL, "EQWindow4");
     HWND eqWindow5 = FindWindow(NULL, "EQWindow5");
     
     //The while loop which waits for a keypress to then switch windows
     while(GetMessage(&msg, 0, 0, 0))
     {
      PeekMessage(&msg, 0, 0, 0, 0x0001);
      switch(msg.message)
      {
      case WM_HOTKEY:
       if(msg.wParam == ZERO_KEYID)
       {
        SetForegroundWindowInternal(eqWindow0);
        printf("Activated Window 0\n");
       }
       if(msg.wParam == ONE_KEYID)
       {
        SetForegroundWindowInternal(eqWindow1);
        printf("Activated Window 1\n");
       }
       if(msg.wParam == TWO_KEYID)
       {
        SetForegroundWindowInternal(eqWindow2);
        printf("Activated Window 2\n");
       }
       if(msg.wParam == THREE_KEYID)
       {
        SetForegroundWindowInternal(eqWindow3);
        printf("Activated Window 3\n");
       }
       if(msg.wParam == FOUR_KEYID)
       {
        SetForegroundWindowInternal(eqWindow4);
        printf("Activated Window 4\n");
       }
       if(msg.wParam == FIVE_KEYID)
       {
        SetForegroundWindowInternal(eqWindow5);
        printf("Activated Window 5\n");
       }
      }
     }
     return 0;
    }
     
  11. mackal

    mackal Pyrilen Fireblade

    Messages:
    2,267
    You can use MQ2's /setwintitle to set the window title, I have mine in config/zoned.cfg I think
     
  12. kosmoss

    kosmoss Orc Pawn

    Messages:
    26

    to use this i need to put code in e3_Loot.inc?
     
  13. Hive

    Hive Froglok Krup Watcher

    Messages:
    340
    It goes in E3 Basics

    Also
    Code:
    /doevents lootItem
    Inside of
    Sub basics_Background_Events
    which is at the bottom of E3 Basics
     
  14. tackleberry

    tackleberry Orc Pawn

    Messages:
    46
    Why cant one of you make an alexa for peq so I can say the chars name and it will go foreground : )
     
    • Like Like x 1