| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- #include "stdafx.h"
- #include "externalwindowtracker.h"
- #include "Misc.h"
- #include "SendKeys.h"
- #include "Options.h"
- #include "CP_Main.h"
- ExternalWindowTracker::ExternalWindowTracker(void)
- {
- m_activeWnd = NULL;
- m_focusWnd = NULL;
- m_dittoHasFocus = false;
- }
- ExternalWindowTracker::~ExternalWindowTracker(void)
- {
- }
- bool ExternalWindowTracker::TrackActiveWnd(HWND focus)
- {
- BOOL fromHook = true;
- HWND newFocus = focus;
- HWND newActive = ::GetForegroundWindow();
- if(newFocus == NULL)
- {
- AttachThreadInput(GetWindowThreadProcessId(newActive, NULL), GetCurrentThreadId(), TRUE);
- newFocus = GetFocus();
- AttachThreadInput(GetWindowThreadProcessId(newActive, NULL), GetCurrentThreadId(), FALSE);
- fromHook = false;
- }
- if(newFocus == 0 && newActive != 0)
- {
- newFocus = newActive;
- }
- else if(newActive == 0 && newFocus != 0)
- {
- newActive = newFocus;
- }
- if(newFocus == 0 || !IsWindow(newFocus) || newActive == 0 || !IsWindow(newActive))
- {
- Log(_T("TargetActiveWindow values invalid"));
- return false;
- }
- if(newFocus == m_focusWnd)
- {
- // Log(_T("TargetActiveWindow window the same"));
- return false;
- }
- TCHAR className[50];
- GetClassName(newFocus, className, (sizeof(className) / sizeof(TCHAR)));
- if(STRCMP(className, _T("Shell_TrayWnd")) == 0)
- {
- Log(_T("TargetActiveWindow shell tray icon has focus"));
- return false;
- }
- GetClassName(newActive, className, (sizeof(className) / sizeof(TCHAR)));
- if(STRCMP(className, _T("Shell_TrayWnd")) == 0)
- {
- Log(_T("TargetActiveWindow shell tray icon has active"));
- return false;
- }
- if(IsAppWnd(newFocus))
- {
- m_dittoHasFocus = true;
- return false;
- }
- m_focusWnd = newFocus;
- m_activeWnd = newActive;
- m_dittoHasFocus = false;
- if(theApp.QPasteWnd())
- theApp.QPasteWnd()->UpdateStatus(true);
- Log(StrF(_T("TargetActiveWindow Active: %d, Focus: %d, FromHook %d"), m_activeWnd, m_focusWnd, fromHook));
- return true;
- }
- bool ExternalWindowTracker::WaitForActiveWnd(HWND activeWnd, int timeout)
- {
- DWORD start = GetTickCount();
- while(((int)(GetTickCount() - start)) < timeout)
- {
- if(::GetForegroundWindow() == activeWnd)
- {
- Log(StrF(_T("found focus wait %d"), GetTickCount()-start));
- return true;
- }
- Sleep(0);
- ActivateTarget();
- }
- Log(_T("Didn't find focus"));
- return false;
- }
- bool ExternalWindowTracker::ActivateTarget()
- {
- Log(StrF(_T("Activate Target - Active: %d Focus: %d"), m_activeWnd, m_focusWnd));
- SetForegroundWindow(m_activeWnd);
- AttachThreadInput(GetWindowThreadProcessId(m_activeWnd, NULL), GetCurrentThreadId(), TRUE);
- SetFocus(m_focusWnd);
- AttachThreadInput(GetWindowThreadProcessId(m_activeWnd, NULL), GetCurrentThreadId(), FALSE);
- return true;
- }
- void ExternalWindowTracker::SendPaste(bool activateTarget)
- {
- CSendKeys send;
- send.AllKeysUp();
- CString csPasteToApp = GetProcessName(m_activeWnd);
- CString csPasteString = g_Opt.GetPasteString(csPasteToApp);
- DWORD delay = g_Opt.SendKeysDelay();
- if(activateTarget)
- {
- ActivateTarget();
- theApp.PumpMessageEx();
- WaitForActiveWnd(m_activeWnd, max(25, g_Opt.WaitForActiveWndTimeout()));
- }
- else
- {
- theApp.PumpMessageEx();
- }
- m_dittoHasFocus = false;
- Log(StrF(_T("Sending paste to app %s key stroke: %s, SeDelay: %d"), csPasteToApp, csPasteString, delay));
- send.SetKeyDownDelay(max(50, delay));
- send.SendKeys(csPasteString, true);
- Log(_T("Post sending paste"));
- }
- void ExternalWindowTracker::SendCopy()
- {
- CSendKeys send;
- send.AllKeysUp();
- CString csToApp = GetProcessName(m_activeWnd);
- CString csString = g_Opt.GetCopyString(csToApp);
- DWORD delay = g_Opt.SendKeysDelay();
- Sleep(delay);
- theApp.PumpMessageEx();
- Log(StrF(_T("Sending copy to app %s key stroke: %s, Delay: %d"), csToApp, csString, delay));
- //give the app some time to take focus before sending paste
- Sleep(delay);
- send.SetKeyDownDelay(max(50, delay));
- send.SendKeys(csString, true);
- Log(_T("Post sending copy"));
- }
- // sends Ctrl-X to the TargetWnd
- void ExternalWindowTracker::SendCut()
- {
- CSendKeys send;
- send.AllKeysUp();
- CString csToApp = GetProcessName(m_activeWnd);
- CString csString = g_Opt.GetCopyString(csToApp);
- DWORD delay = g_Opt.SendKeysDelay();
- Sleep(delay);
- theApp.PumpMessageEx();
- Log(StrF(_T("Sending cut to app %s key stroke: %s, Delay: %d"), csToApp, csString, delay));
- //give the app some time to take focus before sending paste
- Sleep(delay);
- send.SetKeyDownDelay(max(50, delay));
- send.SendKeys(csString, true);
- Log(_T("Post sending cut"));
- }
- CString ExternalWindowTracker::ActiveWndName()
- {
- TCHAR cWindowText[200];
- HWND hParent = m_activeWnd;
- ::GetWindowText(hParent, cWindowText, 100);
- int nCount = 0;
- while(STRLEN(cWindowText) <= 0)
- {
- hParent = ::GetParent(hParent);
- if(hParent == NULL)
- break;
- ::GetWindowText(hParent, cWindowText, 100);
- nCount++;
- if(nCount > 100)
- {
- Log(_T("GetTargetName reached maximum search depth of 100"));
- break;
- }
- }
- return cWindowText;
- }
- bool ExternalWindowTracker::ReleaseFocus()
- {
- if( IsAppWnd(::GetForegroundWindow()) )
- {
- return ActivateTarget();
- }
- return false;
- }
- CPoint ExternalWindowTracker::FocusCaret()
- {
- CPoint pt(-1, -1);
- if(m_activeWnd)
- {
- GUITHREADINFO guiThreadInfo;
- guiThreadInfo.cbSize = sizeof(GUITHREADINFO);
- DWORD OtherThreadID = GetWindowThreadProcessId(m_activeWnd, NULL);
- if(GetGUIThreadInfo(OtherThreadID, &guiThreadInfo))
- {
- CRect rc(guiThreadInfo.rcCaret);
- if(rc.IsRectEmpty() == FALSE)
- {
- pt = rc.BottomRight();
- ::ClientToScreen(m_focusWnd, &pt);
- }
- }
- }
- return pt;
- }
|