diff --git a/src/conio/x_cio.c b/src/conio/x_cio.c
index 9a1ad5c1c1e3c21abe734f67efee9c8176d27033..51a8daabd5df07f9191ffba87ea41d5d36d29817 100644
--- a/src/conio/x_cio.c
+++ b/src/conio/x_cio.c
@@ -446,6 +446,10 @@ int x_initciolib(int mode)
 		xp_dlclose(dl);
 		return(-1);
 	}
+	if((x11.XUnsetICFocus=xp_dlsym(dl,XUnsetICFocus))==NULL) {
+		xp_dlclose(dl);
+		return(-1);
+	}
 	if((x11.XFilterEvent=xp_dlsym(dl,XFilterEvent))==NULL) {
 		xp_dlclose(dl);
 		return(-1);
diff --git a/src/conio/x_events.c b/src/conio/x_events.c
index f057eee8ddcb05dc6c960bf50393f63653021d30..bbf33d2c3c67a27b3b40b800207e566b33f8bd19 100644
--- a/src/conio/x_events.c
+++ b/src/conio/x_events.c
@@ -1069,9 +1069,10 @@ static int init_window()
 	gcv.graphics_exposures = False;
 	gc=x11.XCreateGC(dpy, win, GCFunction | GCForeground | GCBackground | GCGraphicsExposures, &gcv);
 
-	x11.XSelectInput(dpy, win, KeyReleaseMask | KeyPressMask |
-		     ExposureMask | ButtonPressMask | PropertyChangeMask
-		     | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
+	x11.XSelectInput(dpy, win, KeyReleaseMask | KeyPressMask
+		     | ExposureMask | ButtonPressMask | PropertyChangeMask
+		     | ButtonReleaseMask | PointerMotionMask
+		     | StructureNotifyMask | FocusChangeMask);
 
 	x11.XStoreName(dpy, win, "SyncConsole");
 	Atom protos[2];
@@ -1732,6 +1733,29 @@ x11_event(XEvent *ev)
 				expose_rect(ev->xexpose.x, ev->xexpose.y, ev->xexpose.width, ev->xexpose.height);
 			break;
 
+		/* Focus Events */
+		case FocusIn:
+		case FocusOut:
+			{
+				if (ev->xfocus.mode == NotifyGrab)
+					break;
+				if (ev->xfocus.mode == NotifyUngrab)
+					break;
+				if (ev->xfocus.detail == NotifyInferior)
+					break;
+				if (ev->xfocus.detail == NotifyPointer)
+					break;
+				if (ev->type == FocusIn) {
+					if (ic)
+						x11.XSetICFocus(ic);
+				}
+				else {
+					if (ic)
+						x11.XUnsetICFocus(ic);
+				}
+				break;
+			}
+
 		/* Copy/Paste events */
 		case SelectionClear:
 			{
diff --git a/src/conio/x_events.h b/src/conio/x_events.h
index 524327bb34a83e54881a6f779fbb68f12baa079f..51b5b3cd228a494dbd5705b377519c2e68ae47e7 100644
--- a/src/conio/x_events.h
+++ b/src/conio/x_events.h
@@ -105,6 +105,7 @@ struct x11 {
 	XIC (*XCreateIC)(XIM im, ...);
 	int (*XwcLookupString)(XIC ic, XKeyPressedEvent *event, wchar_t *buffer_return, int wchars_buffer, KeySym *keysym_return, Status *status_return);
 	void (*XSetICFocus)(XIC ic);
+	void (*XUnsetICFocus)(XIC ic);
 	Bool (*XFilterEvent)(XEvent *event, Window w);
 	Cursor (*XCreateFontCursor)(Display *display, unsigned int shape);
 	int (*XDefineCursor)(Display *display, Window w, Cursor cursor);