I need to send Shift+A keystrokes to get capital A. The only problem: I can't use XTestFakeKeyEvent (because it's an extension) and I don't want to pass Shift through an event.state (if possible). I tried 4 calls to XSendEvent which doesn't work. So I wonder: how XTestFakeKeyEvent does the same thing?
P.S: "my" code
// Send a fake keystroke event to an X window.
// by Adam Pierce - http://www.doctort.org/adam/
#include <X11/Xlib.h>
#include <X11/keysym.h>
// Function to create a keyboard event
XKeyEvent createKeyEvent(Display *display, Window &win,
Window &winRoot, bool press,
int keycode, int modifiers)
{
XKeyEvent event;
event.display = display;
event.window = win;
event.root = winRoot;
event.subwindow = None;
event.time = CurrentTime;
event.x = 1;
event.y = 1;
event.x_root = 1;
event.y_root = 1;
event.same_screen = True;
event.keycode = XKeysymToKeycode(display, keycode);
event.state = modifiers; // I know here I can pass Shift explicitly
// But is there a better way?
if(press)
event.type = KeyPress;
else
event.type = KeyRelease;
return event;
}
main()
{
// Obtain the X11 display.
Display *display = XOpenDisplay(0);
if(display == NULL)
return -1;
// Get the root window for the current display.
Window winRoot = XDefaultRootWindow(display);
// Find the window which has the current keyboard focus.
Window winFocus;
int revert;
XGetInputFocus(display, &winFocus, &revert);
// Send a fake key press event to the window.
XKeyEvent event = createKeyEvent(display, winFocus, winRoot, true, XK_A, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
// Send a fake key press event to the window.
event = createKeyEvent(display, winFocus, winRoot, true, XK_Shift_L, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
// Send a fake key release event to the window.
event = createKeyEvent(display, winFocus, winRoot, false, XK_Shift_L, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
// Send a fake key release event to the window.
event = createKeyEvent(display, winFocus, winRoot, false, XK_A, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
// Done.
XCloseDisplay(display);
return 0;
}
Related
im trying to just make a button in the middle of the screen but i cant check if the mouse is inside the sfRectangleShape
sfRectangleShape* newButton(sfVector2f size, sfVector2f position, sfColor color)
{
sfRectangleShape *newButton;
newButton = sfRectangleShape_create();
sfRectangleShape_setSize(newButton, size);
sfRectangleShape_setOrigin(newButton, (sfVector2f){sfRectangleShape_getSize(newButton).x / 2, sfRectangleShape_getSize(newButton).y / 2});
sfRectangleShape_setFillColor(newButton, color);
sfRectangleShape_setPosition(newButton, position);
return newButton;
}
int main()
{
sfRenderWindow* window;
sfView *view = sfView_create();
window = sfRenderWindow_create((sfVideoMode){600, 400, 32}, "", sfResize | sfClose, NULL);
sfRectangleShape *screenButton = newButton((sfVector2f){100, 35}, (sfVector2f){300, 200}, rgba(72, 74, 89));
sfVector2i mouse_pos;
while (sfRenderWindow_isOpen(window))
{
sfRenderWindow_mapPixelToCoords(window, mouse_pos, view);
sfEvent event;
while (sfRenderWindow_pollEvent(window, &event))
{
/* Close window : exit */
if (event.type == sfEvtClosed)
sfRenderWindow_close(window);
}
printf("%d, %d", mouse_pos.x, mouse_pos.y);
sfRenderWindow_clear(window, sfColor_fromRGBA(82, 84, 100, 1));
sfRenderWindow_drawRectangleShape(window, screenButton, NULL);
sfRenderWindow_display(window);
}
return 0;
}
i cant find a example or a tutorial that teach how to detect if my mouse are inside the sfRectangleShape and the function sfRenderWindow_mapPixelToCoords just return the number -40270372832704
You could use sfMouse_getPosition(window) to get the mouse coordinates, which returns as sfVector2i
As Kolbjørn Christiansen said you can use sfMouse_getPosition(window) to get the position of the mouse on your window (not screen. if you want screen you have to remove window from the function arg: sfMouse_getPosition()).
I want to add that this function returns a vector 2f, which is an object containing x and y coords. You can also use with accordance to this the globalbounds function contains to see if that coord is inside that object, a full workup of the solution would be:
sf::RectangleShape rect;
//init rect sizes position etc.
//...
if (rect.getGlobalBounds().contains(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y))
{
//means mouse is in rect
}
else
{
//mouse is not in rect
}
I'm modifying an existing program wrote in C in which I added a GtkEntry.
When I try to write, for example, "qwerty" in the entry, it's filled only with "qwrty" because the character "e" is used as a shortcut (accelerator) to call another function, and that function is also activated when "e" is pressed.
Is there any way to avoid accelerator callbacks while we are writing in the entry?
Yes, you can disconnect your accelerator group in the focus-in-event callback of GtkEntry, and connect it again when you focus back out of the entry. Here's an example:
#include <stdio.h>
#include <gtk/gtk.h>
GtkAccelGroup *accel_group;
GClosure *closure;
void accelerator_pressed(void)
{
printf("Accelerator pressed!\n");
}
gboolean focus_in_callback(void)
{
gtk_accel_group_disconnect(accel_group, closure);
g_closure_unref(closure);
return GDK_EVENT_PROPAGATE;
}
gboolean focus_out_callback(void)
{
closure = g_cclosure_new(accelerator_pressed, 0, 0);
gtk_accel_group_connect(accel_group, GDK_KEY_e, (GdkModifierType)0, GTK_ACCEL_VISIBLE, closure);
return GDK_EVENT_PROPAGATE;
}
int main()
{
gtk_init(NULL, NULL);
GtkWidget *window, *box, *entry, *button;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
entry = gtk_entry_new();
button = gtk_button_new_with_label("click me");
gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(entry), TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(button), TRUE, TRUE, 0);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(GTK_WIDGET(entry), "focus-in-event", G_CALLBACK(focus_in_callback), NULL);
g_signal_connect(GTK_WIDGET(entry), "focus-out-event", G_CALLBACK(focus_out_callback), accel_group);
accel_group = gtk_accel_group_new();
gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
gtk_container_add(GTK_CONTAINER(window), box);
gtk_widget_show_all(window);
gtk_main();
}
After enabling a disabled child window, I try to turn mouse tracking on in WM_ENABLE only if mouse cursor is hovering over the window using TrackMouseEvent() with dwFlags of TRACKMOUSEEVENT structure set to TME_LEAVE. TrackMouseEvent() returns TRUE, but then right after calling it I get a WM_MOUSELEAVE message. This happens only under 2 conditions. With first condition, move cursor outside of child window, press Enter key to disable the window, then move cursor over the child window and press the Space key. With second condition, move the cursor over window, press Enter key to disable it, then before pressing the Space key move the cursor 1 pixel or more and then press the Space key. If you retest the second condition, but instead of moving cursor before you press the Space key, if you press the Space key right after you press the Enter key, mouse tracking is turned on properly. I've tried really hard to fix this but I've not been lucky so far. Can somebody please fix this code and explain why mouse tracking is being canceled when I'm trying to turn it on?
#include <windows.h>
const WCHAR g_szChildClassName[] = L"Childclass////";
HINSTANCE g_hInst;
LRESULT CALLBACK ChildProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static BOOL bMouseTracking = FALSE;
switch(msg)
{
case WM_PAINT:
{
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
if(hdc)
{
HBRUSH hbr = CreateSolidBrush(bMouseTracking?RGB(255, 0, 0):RGB(0, 0, 255));
if(hbr)
{
FillRect(hdc, &ps.rcPaint, hbr);
DeleteObject(hbr);
}
EndPaint(hwnd, &ps);
}
}
break;
case WM_MOUSEMOVE:
if(!bMouseTracking)
{
TRACKMOUSEEVENT tme = { 0 };
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = hwnd;
bMouseTracking = TrackMouseEvent(&tme);
InvalidateRect(hwnd, 0, TRUE);
}
break;
case WM_MOUSELEAVE:
bMouseTracking = FALSE;
InvalidateRect(hwnd, 0, TRUE);
break;
case WM_ENABLE:
if(wParam)
{
RECT rc;
if(GetWindowRect(hwnd, &rc))
{
POINT pt;
if(GetCursorPos(&pt))
if(PtInRect(&rc, pt))
{
TRACKMOUSEEVENT tme = { 0 };
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = hwnd;
//TrackMouseEvent() posts WM_MOUSELEAVE if conditions 1 and 2 are met, even though I'm trying to turn
//mouse tracking on and the cursor is over the child window. It doesn't make sense
//The problems is this piece of code right here /* bMouseTracking = TrackMouseEvent(&tme); */
//It should turn tracking on but it doesn't it cancels it even though WS_DISABLED has already been removed
//at this point
bMouseTracking = TrackMouseEvent(&tme);
InvalidateRect(hwnd, 0, TRUE);
}
}
} else {
if(bMouseTracking) {
////////If you comment everything from here ...
TRACKMOUSEEVENT tme = { 0 };
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE | TME_CANCEL;
tme.hwndTrack = hwnd;
//if(TrackMouseEvent(&tme)) PostMessage(hwnd, WM_MOUSELEAVE, 0, 0); //Commented this line out to do things a bit differently with the same results
if(TrackMouseEvent(&tme)) { //If this succeeds it means mouse tracking was canceled
bMouseTracking = FALSE;
InvalidateRect(hwnd, 0, TRUE);
}
////////all the way down to here the result is the same
//If you comment everything in this block out then you have another problem which can be tested with this condition:
//With window enabled move mouse over window, then press the ENTER key. The color should change
//from red to blue but it doesn't. It will change to blue though if you move the mouse 1 or more pixels after you've pressed the ENTER key
}
}
break;
case WM_DESTROY:
bMouseTracking = FALSE;
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hChild;
switch(msg)
{
case WM_CREATE:
hChild = CreateWindowEx(0, g_szChildClassName, 0, WS_VISIBLE | WS_CHILD, 4, 4, 240, 80, hwnd, 0, g_hInst, 0);
break;
case WM_KEYDOWN:
if(wParam == VK_SPACE) EnableWindow(hChild, TRUE);
else if(wParam == VK_RETURN) EnableWindow(hChild, FALSE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
const TCHAR szClassName[] = L"abccccccc";
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
SecureZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = szClassName;
if(!RegisterClassEx(&wc)) return 0; //Register main window class
SecureZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hInstance = hInstance;
wc.lpfnWndProc = ChildProc;
wc.lpszClassName = g_szChildClassName;
if(!RegisterClassEx(&wc)) return 0; //Register child window class
g_hInst = hInstance;
hwnd = CreateWindowEx(0, szClassName, L"Test", WS_OVERLAPPEDWINDOW, 40, 40, 420, 200, 0, 0, hInstance, 0);
if(!hwnd) return 0;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, 0, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
EDIT: You can't see the cursor in the pictures cause I used screen capture and it doesn't capture the cursor. In the first picture the cursor is outside of the child window and in the second picture the cursor is inside of the child window
ENTER key pressed when cursor is outside of child window
SPACE key pressed after the ENTER key was previously pressed and cursor is hovering over child window
I don't know if this is a shortcoming of the documentation or a bug in TrackMouseEvent, but it looks like TrackMouseEvent doesn't expect to be called within your WM_ENABLE handler.
To avoid that, try posting a message from WM_ENABLE and calling TrackMouseEvent from that:
case WM_ENABLE:
PostMessage(hwnd, WM_USER, wParam, lParam);
break;
case WM_USER:
if(wParam)
{
RECT rc;
if(GetWindowRect(hwnd, &rc))
{
POINT pt;
if(GetCursorPos(&pt))
if(PtInRect(&rc, pt))
{
TRACKMOUSEEVENT tme = { 0 };
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = hwnd;
//TrackMouseEvent() posts WM_MOUSELEAVE if conditions 1 and 2 are met, even though I'm trying to turn
//mouse tracking on and the cursor is over the child window. It doesn't make sense
//The problems is this piece of code right here /* bMouseTracking = TrackMouseEvent(&tme); */
//It should turn tracking on but it doesn't it cancels it even though WS_DISABLED has already been removed
//at this point
bMouseTracking = TrackMouseEvent(&tme);
InvalidateRect(hwnd, 0, TRUE);
}
}
} else {
if(bMouseTracking) {
////////If you comment everything from here ...
TRACKMOUSEEVENT tme = { 0 };
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE | TME_CANCEL;
tme.hwndTrack = hwnd;
//if(TrackMouseEvent(&tme)) PostMessage(hwnd, WM_MOUSELEAVE, 0, 0); //Commented this line out to do things a bit differently with the same results
if(TrackMouseEvent(&tme)) { //If this succeeds it means mouse tracking was canceled
bMouseTracking = FALSE;
InvalidateRect(hwnd, 0, TRUE);
}
////////all the way down to here the result is the same
//If you comment everything in this block out then you have another problem which can be tested with this condition:
//With window enabled move mouse over window, then press the ENTER key. The color should change
//from red to blue but it doesn't. It will change to blue though if you move the mouse 1 or more pixels after you've pressed the ENTER key
}
}
break;
I have a scrolledwindow inside a main window. I want to when i click button refresh, content of scrolledwindow will refresh automatic.This is callback to button
vbox = gtk_vbox_new(TRUE, 5);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), vbox);
gtk_signal_connect(GTK_OBJECT(button_refresh), "clicked", GTK_SIGNAL_FUNC(button_re), NULL);
there is callback function:
void button_re(GtkWidget *window, gpointer data){
connectserver(myFile, numof);//connect to server and get information
if(numof > 0){
for(int i = 0; i< numof; i++){
hbox = gtk_hbox_new(TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
sprintf(buffer, "%s", myFile[i].name);
label = gtk_label_new(buffer);
button_down = gtk_button_new_with_label("Download");
gtk_signal_connect(GTK_OBJECT(button_down), "clicked", GTK_SIGNAL_FUNC(button_download), (gpointer ) i);
gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), button_down, TRUE, FALSE, 0);
}
}else if(numof == 0){
label = gtk_label_new("Have nothing on server");
gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, FALSE, 2);
}
But when i click button, there are nothing happed. What should i do?
I'm so sorry because my english is not good.
Thanks !
In button_re you are creating new labels, buttons, etc. These won't show up until you call gtk_widget_show on them, somehow. Somewhere in the initialization of your program - main() perhaps - probably you call gtk_widget_show_all on your main window, which recursively "show"s everything within it. But these new objects won't be shown until you explicitly request that they are.
I want to make parent window as non transparent with RGB value as (99,99,99)? Previously my window was transparent but now i have requirement to make window as non transparent.
Mentioned below are the function related to my parent window:
ATOM MyRegisterClass(HINSTANCE hInstance)
{
LogEntry(L"Entered in myRegisterClass Function");
WNDCLASS CLASS_NAME_ONE_SEG_APP;
CLASS_NAME_ONE_SEG_APP.cbClsExtra = 0;
CLASS_NAME_ONE_SEG_APP.cbWndExtra = 0;
CLASS_NAME_ONE_SEG_APP.hbrBackground = 0;
CLASS_NAME_ONE_SEG_APP.hCursor = 0;
CLASS_NAME_ONE_SEG_APP.hIcon = 0;
CLASS_NAME_ONE_SEG_APP.hInstance = hInstance;
CLASS_NAME_ONE_SEG_APP.lpfnWndProc = (WNDPROC) WndProc;
CLASS_NAME_ONE_SEG_APP.lpszClassName = className;
CLASS_NAME_ONE_SEG_APP.lpszMenuName = 0;
CLASS_NAME_ONE_SEG_APP.style = 0;
LogEntry(L"Exiting from myRegisterClass Function");
return RegisterClass(&CLASS_NAME_ONE_SEG_APP);
}
Mentioned below is an InitInstance function in which i am creating the parent window.
handles.parent is my parent window.
bool WINAPI InitInstance(HINSTANCE hInstance, int nCmdShow)
{
LogEntry(L"Entered in InitInstance Function");
handles.parent = CreateWindowEx(0,
className,
windowName,
WS_VISIBLE | WS_POPUP,
0, 0,
coordinates.width, coordinates.height,
NULL,
NULL,
hInstance,
NULL);
if(handles.parent == NULL)
{
LogValue(L"Cannot Create Parent Window"
L"\nInitInstance Function terminated abnormally");
return false;
}
else
{
UpdateWindow(handles.parent);
ShowWindow(handles.parent, nCmdShow);
LogEntry(L"Exiting from InitInstance Function");
return true;
}
}
The below mentioned is a function for WM_PAINT:
case WM_PAINT:
LogEntry(L"Entred in WM_PAINT");
PaintWindow();
SetFocus(handles.parent);
LogEntry(L"Exited from WM_PAINT");
break;
This PaintWindow does the following.....
void PaintWindow()
{
LogEntry(L"Entered in PaintWindow Function");
HWND *handle = &handles.volUp;
//Paint Buttons on the window
for(register char i = MIN_BUTTON; i <= MAX_BUTTON; i++)
{
PaintButton( (INITIAL_BUTTON + i) , *handle, btns, i);
handle++;
}
//Paint the AXIS_VOL_ON_OFF Button According to its Status
if(volumeStatus.status == VOLUME_ON)
PaintButton(IDB_BTN_VOL_OFF, handles.volOnOff, btns, AXIS_VOL_ON_OFF);
else if(volumeStatus.status = VOLUME_MUTE)
PaintButton(IDB_BTN_VOL_ON, handles.volOnOff, btns, AXIS_VOL_ON_OFF);
//Paint Images on the window
if(handles.screenMode == SCREEN_MODE_OPERATION)
InsertImages();
LogEntry(L"Exited from PaintWindow Function");
}
Thanks in advance......
You need to give your class a non-null background brush
CLASS_NAME_ONE_SEG_APP.hbrBackground = CreateSolidBrush(RGB((99,99,99));
And in your WindowProc, you need to make sure that you pass the WM_ERASEBKGND message to DefWindowProc. (that part is probably already happening)
Later
Ok, your WM_PAINT code is doing some things wrong. It's important that you call BeginPaint and EndPaint when you handle the WM_PAINT message, and you have to use the HDC that you get from BeginPaint when you draw.
case WM_PAINT:
{
LogEntry(L"Entred in WM_PAINT");
PAINTSTRUCT ps;
HDC hdc = BeginPaint(&ps);
PaintWindow(hdc);
EndPaint(hwnd, &ps);
LogEntry(L"Exited from WM_PAINT");
}
break;
Your window is transparent because you never call BeginPaint in your WM_PAINT handler, so you never end up drawing anything to the screen.
See Drawing in the Client Area for a more complete example.