Sorry for taking so long, here it comes:
The Windows code and screenshot:
Code:
#include <iostream>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
{
WNDCLASSEX windowClass;
HWND hwnd;
MSG msg;
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = NULL;
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "MyClass";
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
if (!RegisterClassEx(&windowClass))
{
MessageBox(0, "Failed to register window!", "Error", 0);
return -1;
}
hwnd = CreateWindowEx(0,
"MyClass",
"Press The Button!",
WS_OVERLAPPEDWINDOW | WS_VISIBLE |
WS_SYSMENU | WS_CLIPCHILDREN |
WS_CLIPSIBLINGS,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if (!hwnd)
{
MessageBox(0, "Failed to create window!", "Error", 0);
return -2;
}
ShowWindow(hwnd, SW_SHOW);
while (GetMessage(&msg, NULL, 0, 0) != WM_QUIT)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_KEYDOWN:
if (wParam == 'W')
std::cout << "Button Pressed!\n";
break;
case WM_KEYUP:
if (wParam == 'W')
std::cout << "Button Released!\n";
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
And here's Linux's:
Code:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
int main()
{
XVisualInfo* visual = 0;
Window window = 0;
XSetWindowAttributes attributes;
memset(&attributes, 0, sizeof(attributes));
Display* display;
int screennr;
display = XOpenDisplay(0);
if (!display)
return -1;
screennr = DefaultScreen(display);
XVisualInfo visTemp;
int visNumber;
visTemp.screen = screennr;
visTemp.depth = 24;
while (!visual && visTemp.depth >= 16)
{
visual = XGetVisualInfo(display, VisualScreenMask | VisualDepthMask, &visTemp, &visNumber);
visTemp.depth -= 8;
}
Colormap colormap;
colormap = XCreateColormap(display,
RootWindow(display, visual->screen),
visual->visual, AllocNone);
attributes.colormap = colormap;
attributes.border_pixel = 0;
attributes.event_mask = StructureNotifyMask | FocusChangeMask | ExposureMask;
attributes.event_mask |= PointerMotionMask |
ButtonPressMask | KeyPressMask |
ButtonReleaseMask | KeyReleaseMask;
attributes.override_redirect = 0;
window = XCreateWindow(display, RootWindow(display, visual->screen),
0, 0, 800, 600, 0, visual->depth,
InputOutput, visual->visual,
CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect,
&attributes);
XMapRaised(display, window);
for (;;)
{
XEvent event;
XNextEvent(display, &event);
switch (event.type)
{
case KeyPress:
{
long unsigned int X11Key;
char buf[8]={0};
XLookupString(&event.xkey, buf, sizeof(buf), &X11Key, NULL);
if (X11Key == XK_w)
std::cout << "Button Pressed!\n";
}
break;
case KeyRelease:
{
long unsigned int X11Key;
char buf[8]={0};
XLookupString(&event.xkey, buf, sizeof(buf), &X11Key, NULL);
if (X11Key == XK_w)
std::cout << "Button Released!\n";
}
break;
}
}
return 0;
}
In both I kept the button pressed as long as needed to fill the console with new data. You can see that the Linux case is hopeless... Am I doing something wrong?