Using GetSystemTime in WM_KEYDOWN
-
I must be doing something really silly but I'm trying to get the time information when a key is hit down then released (WM_KEYUP). I'm writing a win32 application (doesn't anyone write these anymore?), but when I hit the key, I get a blank messagebox or squares. What am I doing wrong. Any good advice might win you a keg of cool beer or at least, a thanks. case WM_KEYDOWN: { SYSTEMTIME st; GetSystemTime(&st); MessageBox(NULL,(LPCWSTR)L"Time is "+ st.wMilliseconds,(LPCWSTR)L"Keystroke Analysis",MB_OK); } break; //etc
-
I must be doing something really silly but I'm trying to get the time information when a key is hit down then released (WM_KEYUP). I'm writing a win32 application (doesn't anyone write these anymore?), but when I hit the key, I get a blank messagebox or squares. What am I doing wrong. Any good advice might win you a keg of cool beer or at least, a thanks. case WM_KEYDOWN: { SYSTEMTIME st; GetSystemTime(&st); MessageBox(NULL,(LPCWSTR)L"Time is "+ st.wMilliseconds,(LPCWSTR)L"Keystroke Analysis",MB_OK); } break; //etc
You cannot add a
WORD
type to a constant string. Use theFormat
method of theCString
class to generate the Text for theMessageBox
.«_Superman_»
-
I must be doing something really silly but I'm trying to get the time information when a key is hit down then released (WM_KEYUP). I'm writing a win32 application (doesn't anyone write these anymore?), but when I hit the key, I get a blank messagebox or squares. What am I doing wrong. Any good advice might win you a keg of cool beer or at least, a thanks. case WM_KEYDOWN: { SYSTEMTIME st; GetSystemTime(&st); MessageBox(NULL,(LPCWSTR)L"Time is "+ st.wMilliseconds,(LPCWSTR)L"Keystroke Analysis",MB_OK); } break; //etc
EvScott wrote:
doesn't anyone write these anymore?
Yes. :rolleyes:
case WM_KEYDOWN:
{SYSTEMTIME st;
GetSystemTime(&st);
const int BUFSIZE = 0x100;
TCHAR msg[BUFSIZE];
HRESULT hr = StringCbPrintf(msg, sizeof(TCHAR) * BUFSIZE, _T("Time is %d"), st.wMilliseconds);if (SUCCEEDED(hr)) MessageBox(NULL, msg, _T("Keystroke Analysis"),MB_OK);
}
break;:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You cannot add a
WORD
type to a constant string. Use theFormat
method of theCString
class to generate the Text for theMessageBox
.«_Superman_»
-
EvScott wrote:
doesn't anyone write these anymore?
Yes. :rolleyes:
case WM_KEYDOWN:
{SYSTEMTIME st;
GetSystemTime(&st);
const int BUFSIZE = 0x100;
TCHAR msg[BUFSIZE];
HRESULT hr = StringCbPrintf(msg, sizeof(TCHAR) * BUFSIZE, _T("Time is %d"), st.wMilliseconds);if (SUCCEEDED(hr)) MessageBox(NULL, msg, _T("Keystroke Analysis"),MB_OK);
}
break;:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Thanks,CPallini.I still get the same values when I include your example. However, when I run in debug mode and examine the values, I can see the SYSTEMTIME.wMilliSeconds. Just don't seem able to access it in a messagebox.
I made a test, my code works on my system. Could you please post your updated code? Are you doing a
UNICODE
build? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I made a test, my code works on my system. Could you please post your updated code? Are you doing a
UNICODE
build? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Thanks CPallini - Just don't seem to get a sensible output. I've omitted your code for reasons of clarity. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { SYSTEMTIME st; PAINTSTRUCT ps; switch (message) { case WM_PAINT: { HDC hdc = BeginPaint(hWnd,&ps); EndPaint(hWnd,&ps); } return(0); break; case WM_KEYDOWN: { SYSTEMTIME st; GetSystemTime(&st); //get duration in milliseconds } break; case WM_TIMER: { } break; case WM_KEYUP: { SYSTEMTIME st; GetSystemTime(&st); //get duration in milliseconds } break; case WM_DESTROY: // onload from memory PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
-
Thanks CPallini - Just don't seem to get a sensible output. I've omitted your code for reasons of clarity. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { SYSTEMTIME st; PAINTSTRUCT ps; switch (message) { case WM_PAINT: { HDC hdc = BeginPaint(hWnd,&ps); EndPaint(hWnd,&ps); } return(0); break; case WM_KEYDOWN: { SYSTEMTIME st; GetSystemTime(&st); //get duration in milliseconds } break; case WM_TIMER: { } break; case WM_KEYUP: { SYSTEMTIME st; GetSystemTime(&st); //get duration in milliseconds } break; case WM_DESTROY: // onload from memory PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
Are you doing a UNICODE build? What is
msg
value at runtime, after theStringCbPrintf
call (use the debugger to see)? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Are you doing a UNICODE build? What is
msg
value at runtime, after theStringCbPrintf
call (use the debugger to see)? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Thank you so much CPallini for you contribution. I made a small change to your original code by changing _T to L"" and it worked. Below is your code that I made small changes to. HRESULT hr = StringCbPrintf(msg, sizeof(TCHAR) * BUFSIZE, L"Time is %d", st.wMilliseconds); So I suppose all that's left is to ship that crate of cool beer to ya as promised in my original post. :laugh:
-
Thank you so much CPallini for you contribution. I made a small change to your original code by changing _T to L"" and it worked. Below is your code that I made small changes to. HRESULT hr = StringCbPrintf(msg, sizeof(TCHAR) * BUFSIZE, L"Time is %d", st.wMilliseconds); So I suppose all that's left is to ship that crate of cool beer to ya as promised in my original post. :laugh:
EvScott wrote:
I made a small change to your original code by changing _T to L"" and it worked.
Uhm, soooooooooo strange!
EvScott wrote:
So I suppose all that's left is to ship that crate of cool beer to ya as promised in my original post.
:beer: :-D :beer:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]