Browse Source

New code for awaitKeyReleases

Brian Fiete 3 years ago
parent
commit
ca2301bfd3
2 changed files with 48 additions and 12 deletions
  1. 44 10
      BeefySysLib/platform/win/WinBFApp.cpp
  2. 4 2
      BeefySysLib/platform/win/WinBFApp.h

+ 44 - 10
BeefySysLib/platform/win/WinBFApp.cpp

@@ -278,7 +278,8 @@ WinBFWindow::WinBFWindow(BFWindow* parent, const StringImpl& title, int x, int y
 	mAlphaMaskHeight = 0;
 	mNeedsStateReset = false;	
 	mAwaitKeyReleases = false;
-	mAwaitKeyEventTick = 0;
+	mAwaitKeyReleasesEventTick = 0;
+	mAwaitKeyReleasesCheckIdx = 0;
 	mFocusLostTick = ::GetTickCount();
 
 	if (windowFlags & BFWINDOW_DEST_ALPHA)
@@ -374,6 +375,20 @@ void WinBFWindow::LostFocus(BFWindow* newFocus)
 	}	
 }
 
+void WinBFWindow::GotFocus()
+{
+	DWORD tickNow = ::GetTickCount();
+
+	//OutputDebugStrF("GotFocus since lost %d\n", tickNow - mFocusLostTick);
+
+	if (tickNow - mFocusLostTick >= 1000)
+	{
+		mAwaitKeyReleases = true;
+		mAwaitKeyReleasesCheckIdx = 0;
+		mAwaitKeyReleasesEventTick = ::GetTickCount();
+	}			
+}
+
 void WinBFWindow::SetForeground()
 {
 	bool hadFocus = mHasFocus;
@@ -388,11 +403,7 @@ void WinBFWindow::SetForeground()
 
 	::SetFocus(mHWnd);
 	::SetForegroundWindow(mHWnd);
-	if ((!hadFocus) && (::GetTickCount() - prevFocusLostTick >= 1000))
-	{
-		mAwaitKeyReleases = true;
-		mAwaitKeyEventTick = ::GetTickCount();
-	}
+	
 
 	//OutputDebugStrF("SetForeground %p %d %d %d\n", mHWnd, hadFocus, ::GetTickCount() - prevFocusLostTick, mAwaitKeyReleases);
 }
@@ -420,16 +431,20 @@ void WinBFWindow::RehupMouseOver(bool isMouseOver)
 
 bool WinBFWindow::CheckKeyReleases(bool isKeyDown)
 {
+	if (!mHasFocus)
+		GotFocus();
+
 	if (!mAwaitKeyReleases)
 		return true;
 
 	// Time expired with no key presses
-	if ((mAwaitKeyEventTick != 0) && (::GetTickCount() - mAwaitKeyEventTick > 120))
+	if ((mAwaitKeyReleasesEventTick != 0) && (mAwaitKeyReleasesCheckIdx == 0) && (::GetTickCount() - mAwaitKeyReleasesEventTick > 150))
 	{
+		//OutputDebugStrF("CheckKeyReleases no initial key press\n");
 		mAwaitKeyReleases = false;
 		return true;
 	}
-	mAwaitKeyEventTick = 0;
+	mAwaitKeyReleasesCheckIdx++;
 
 	bool hasKeyDown = false;
 	uint8 keysDown[256] = { 0 };
@@ -437,8 +452,24 @@ bool WinBFWindow::CheckKeyReleases(bool isKeyDown)
 	for (int i = 0; i < 256; i++)
 		if (keysDown[i] & 0x80)
 			hasKeyDown = true;
+
+	if ((hasKeyDown) && (::GetTickCount() - mAwaitKeyReleasesEventTick >= 600))
+	{
+		String dbgStr = "CheckKeyReleases timeout. Keys down:";
+		for (int i = 0; i < 256; i++)
+			if (keysDown[i] & 0x80)
+				dbgStr += StrFormat(" %2X", i);
+		dbgStr += "\n";
+		OutputDebugStr(dbgStr);
+		hasKeyDown = false;
+	}
+
 	if (!hasKeyDown)
+	{
 		mAwaitKeyReleases = false;
+		mAwaitKeyReleasesCheckIdx = 0;
+		mAwaitKeyReleasesEventTick = 0;
+	}
 	return !mAwaitKeyReleases;
 }
 
@@ -877,6 +908,8 @@ LRESULT WinBFWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPar
 			break;
 		case WM_SETFOCUS:
 			//OutputDebugStrF("WM_SETFOCUS %p\n", hWnd);
+			if (!mHasFocus)
+				GotFocus();
 			mHasFocus = true;
 			mSoftHasFocus = true;
 			mGotFocusFunc(this);
@@ -1045,10 +1078,11 @@ LRESULT WinBFWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPar
 				}
 				else if ((isFocused) && (!mHasFocus) && (checkNonFake == this))
 				{
+					//OutputDebugStrF("Timer detected got focus %p\r\n", hWnd);
+					GotFocus();
 					mHasFocus = true;
 					mSoftHasFocus = true;
 					mGotFocusFunc(this);
-					//OutputDebugStrF("Timer detected got focus %p\r\n", hWnd);
 				}
 
 				mSoftHasFocus = mHasFocus;
@@ -1204,7 +1238,7 @@ WinBFApp::WinBFApp()
 	mDataDir = mInstallDir;
 	mInMsgProc = false;
 	mDSoundManager = NULL;
-	mDInputManager = NULL;
+	mDInputManager = NULL;	
 }
 
 WinBFApp::~WinBFApp()

+ 4 - 2
BeefySysLib/platform/win/WinBFApp.h

@@ -51,9 +51,10 @@ public:
 	bool					mHasFocus;
 	bool					mSoftHasFocus; // Mostly tracks mHasFocus except for when we get an explicit 'LostFocus' callback	
 	bool					mAwaitKeyReleases;
-	DWORD					mAwaitKeyEventTick;
+	int						mAwaitKeyReleasesCheckIdx;
+	DWORD					mAwaitKeyReleasesEventTick;
 	DWORD					mFocusLostTick;
-	bool					mNeedsStateReset;		
+	bool					mNeedsStateReset;
 	bool					mKeyLayoutHasAltGr;
 
 public:
@@ -61,6 +62,7 @@ public:
 	static LRESULT CALLBACK WindowProcStub(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
 	void					RehupMouseOver(bool isMouseOver);
 	bool					CheckKeyReleases(bool isKeyDown);
+	void					GotFocus();
 
 public:
 	WinBFWindow(BFWindow* parent, const StringImpl& title, int x, int y, int width, int height, int windowFlags);