simulate Alt Tab keyboard press in C++ to launch Fast Switch window - windows

I have an idea for a project which needs to be run on a touch screen device. The idea is to have a button on screen which when pressed switches between open projects. So exactly how the ALT + TAB keyboard shortcut works.
I know the SendKeys::Send() event in C++ can simulate key presses but it doesn't seem to work for me when I try sending ALT + TAB. So is there a way that I can have the window displaying all open programs (just like when ALT TAB is pressed) through C++ ?
PS The project is a Windows application! Windows 7 to begin but hopefully it can be compatible with more Windows systems later.

Assuming C++/CLI since you mentioned SendKeys. SendKeys cannot work reliably because it releases the keys, making the Alt-Tab window disappear. You want to use SendInput() instead and send a keydown for the Alt key and a keydown + up for the Tab key. This code worked well:
#include <windows.h>
#pragma comment(lib, "user32.lib")
...
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
INPUT input = {INPUT_KEYBOARD};
input.ki.wVk = (WORD)Keys::Menu;
UINT cnt = SendInput(1, &input, sizeof(input));
input.ki.wVk = (WORD)Keys::Tab;
if (cnt == 1) cnt = SendInput(1, &input, sizeof(input));
input.ki.dwFlags = KEYEVENTF_KEYUP;
if (cnt == 1) cnt = SendInput(1, &input, sizeof(input));
if (cnt != 1) throw gcnew System::ComponentModel::Win32Exception;
}

Related

How to send hardware level keyboard input by program?

I have been using keybd_event for keyboard input by command line. However, want something that can send hardware level input, just like real keyboard.
Is there anything that suits my purpose?
I am using Windows as operating system.
I once used SendInput to control a game character. Game (icy tower?) was using directx input system (I think?) and somehow it was ignoring keybd_event calls but this method worked. I do not know how close to hardware you need to be but did this the trick for me. I used virtual key codes but turned them into scancodes for this answer.
UINT PressKeyScan(WORD scanCode)
{
INPUT input[1] = {0};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = NULL;
input[0].ki.wScan = scanCode;
input[0].ki.dwFlags = KEYEVENTF_SCANCODE;
UINT ret = SendInput(1, input, sizeof(INPUT));
return ret;
}
UINT ReleaseKeyScan(WORD scanCode)
{
INPUT input[1] = {0};
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = NULL;
input[0].ki.wScan = scanCode;
input[0].ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
UINT ret = SendInput(1, input, sizeof(INPUT));
return ret;
}
To simulate press and release you use them sequentially (or you may create a separate function for press and release that use same INPUT structure).
WORD scanCodeSpace = 0x39;
PressKeyScan(scanCodeSpace);
ReleaseKeyScan(scanCodeSpace)
You can use MapVirtualKeyA to get scan code from virtual key code.
keybd_event() is deprecated, use SendInput() instead.
SendInput() posts its simulated events to the same queue that the hardware driver posts its events to, as shown in the below diagram from Raymond Chen's blog:
When something gets added to a queue, it takes time for it to come out the front of the queue

SendInput with unicode string not properly sent into UWP app on Windows 10

I have an app that used to send unicode string (not keystroke) thanks to the SendInput api on Windows XP and Windows 7. I tried it on Windows 10 and noticed that if the target app is UWP (like Edge, OneNote, etc...), the Unicode string is not always properly handled. For example, if the app calls SendInput with 'hello' string, then the app shows either 'hello', 'he', or even nothing.
Thanks for advices
Here is the code, based on Qt integration:
QString text;
...
int i = 0;
while (i < result.length())
{
...
// Check if some char needs to be filtered out or converted
...
INPUT Input;
Input.type = INPUT_KEYBOARD;
Input.ki.time = 0;
Input.ki.dwExtraInfo = 0;
Input.ki.wVk = 0;
Input.ki.wScan = text.mid(i, 1).utf16()[0];
Input.ki.dwFlags = KEYEVENTF_UNICODE;
SendInput(1, &Input, sizeof(INPUT));
Input.ki.dwFlags |= KEYEVENTF_KEYUP;
SendInput(1, &Input, sizeof(INPUT));
i++;
}
The Unicode string is sent char by char to filter or convert any char that cannot be displayed.
UWP Windows are not 'normal' ones, they are built directly on top of COM, so user32 functions like sendinput will not work as expected.
If you want to support some kind of Cross-App communications, please check out AppServices and well as the samples provided by Stefan Wick over at his blog.

Detect modifier key release in X11 root window

The overall goal is like Windows' Alt-Tab, so I will use these keys for explanation. I want:
Press Alt -> press Tab -> [ popup appears ] -> press Tab any time holding Alt -> release Alt -> [ popup disappears ].
I can't detect final Alt release.
Trivial approach: grabbing Alt-Tab:
XGrabKey (dpy,
XKeysymToKeycode(dpy,XK_Tab), Mod1Mask,
root, True, GrabModeAsync, GrabModeAsync);
(full code: http://pastebin.com/K2P65KJn)
Result:
[ Alt pressed ]
[ Tab pressed ]
Pressing Alt-Tab reported
[ Tab released ]
Releasing Alt-Tab reported
[ Alt released ] -> nothing reported
Grabbing both Alt-Tab and Any-Alt:
XGrabKey (dpy,
XKeysymToKeycode(dpy,XK_Tab), Mod1Mask,
root, True, GrabModeAsync, GrabModeAsync);
XGrabKey (dpy,
XKeysymToKeycode(dpy,XK_Alt_L), AnyModifier,
root, True, GrabModeAsync, GrabModeAsync);
(full code: http://pastebin.com/75mD1tjA)
It works!
[ Alt pressed ]
Pressing Alt reported
[ Tab pressed ]
Pressing Alt-Tab reported
[ Tab released ]
Releasing Alt-Tab reported
[ Alt released ]
Releasing Alt-Alt reported
But this hides any Alt combination from any running program.
I can't find the way to push back events which don't belong to us (tried XSendEvent), and overally grabbing Alt from the start looks too invasive.
Grabbing Alt after first Alt-Tab press, then ungrab Alt after its release.
Unfortunately, first Alt release is still not reported:
[ Alt pressed ]
[ Tab pressed ]
Pressing Alt-Tab reported, Alt grabbed here
[ Tab released ]
Releasing Alt-Tab reported
[ Alt released ] -> nothing reported! subsequent Alt press/release are reported, though not useful:
[ Alt pressed ]
Pressing Alt reported
...
Do I need to mess with low-level xinput or there is another way to achieve the goal?
It looks like you won't get a KeyRelease event if you have registered interest in it after the key was pressed.
I can think of two different ways around this.
Select KeyReleaseMask for all windows (and keep track of appearing and disappearing windows); or
Once you know Alt is pressed, poll the keyboard state with XQueryKeyboard every 0.1 second or so until it's released.
I have tested the first method and it seems to be working:
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdbool.h>
#include <stdio.h>
void dowin (Display* dpy, Window win, int reg)
{
Window root, parent;
Window* children;
int nchildren, i;
XSelectInput (dpy, win, reg ? KeyReleaseMask|SubstructureNotifyMask : 0);
XQueryTree (dpy, win, &root, &parent, &children, &nchildren);
for (i = 0; i < nchildren; ++i)
{
dowin (dpy, children[i], reg);
}
XFree(children);
}
int main()
{
Display* dpy = XOpenDisplay(0);
Window win = DefaultRootWindow(dpy);
XEvent ev;
unsigned int alt_modmask = Mod1Mask;
unsigned int ignored_modmask = 0; // stub
KeyCode tab_keycode = XKeysymToKeycode(dpy,XK_Tab);
KeyCode alt_keycode = XKeysymToKeycode(dpy,XK_Alt_L);
dowin (dpy, win, True);
XGrabKey (dpy,
tab_keycode,
alt_modmask | ignored_modmask,
win,
True,
GrabModeAsync, GrabModeAsync);
while(true)
{
ev.xkey.keycode = 0;
ev.xkey.state = 0;
ev.xkey.type = 0;
XNextEvent(dpy, &ev);
switch(ev.type)
{
case KeyPress:
printf ("Press %x: d-%d\n", ev.xkey.window, ev.xkey.state, ev.xkey.keycode);
break;
case KeyRelease:
printf ("Release %x: %d-%d\n", ev.xkey.window, ev.xkey.state, ev.xkey.keycode);
break;
case MapNotify:
printf ("Mapped %x\n", ev.xmap.window);
dowin (dpy, ev.xmap.window, True);
break;
case UnmapNotify:
printf ("Unmapped %x\n", ev.xunmap.window);
dowin (dpy, ev.xunmap.window, False);
break;
default:
printf ("Event type %d\n", ev.type);
break;
}
}
XCloseDisplay(dpy);
return 0;
}

Win32 console problem

is it possible to create a program that works as console application if started from the console and works as windows program (with GUI) when started otherwise?
If it is possible - how can I do that?
regards
Tobias
If you set the program up to build as a GUI program you can then attempt to attach to the console using AttachConsole(). You you attach OK then you were started from a console and you can proceed to redirect your standard handles to the newly attached console.
In this way you can start up and see if you are being started from a console that you can attach to and if so become a console program. If you cant attach you can show a GUI.
I've had some success with this, the main problem I have is redisplaying the command window's prompt when my program exits (which is how normal console programs operate), but I expect you could do something clever (read the console buffer on start up and find the prompt to redisplay when you exit?) if you really wanted to ...
If you need the program to act as a console application (e.g. print the usage information to the console) you must complile as a console application. A windows application will not have access to the console and cmd.exe will not wait for it to finish before printing the prompt and accepting the next command.
The best solution is to have two versions, one for command line and one for the GUI (which users usually run via a link on the desktop or start menu).
If you insist on using a single binary you will have to live with a console window appearing, at least for a short time. You can get rid of the console window using
FreeConsole();
You can tell that your application was run from GUI if it is the only process attached to the console. You can use GetConsoleProcessList to find the list of processes attached to the console.
This is the answer from Dan Tillett and it is remarkably effective. No flashes, no .com and .exe to trick cmd.exe. Seems to work flawlessly typing the command, in a .bat file, with focus, without focus and as double-click GUI app.
It's the bees knees!
Here is web page describing it, but I've posted it here because if that page goes 404 next month or 2 years from now, the excellent and "most complete" solution I've seen would be "off the grid".
http://www.tillett.info/2013/05/13/how-to-create-a-windows-program-that-works-as-both-as-a-gui-and-console-application/
#define WINVER 0x0501 // Allow use of features specific to Windows XP or later.
#define _WIN32_WINNT 0x0501
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#pragma comment(lib, "User32.lib")
// Attach output of application to parent console
static BOOL attachOutputToConsole(void) {
HANDLE consoleHandleOut, consoleHandleError;
int fdOut, fdError;
FILE *fpOut, *fpError;
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
//redirect unbuffered STDOUT to the console
consoleHandleOut = GetStdHandle(STD_OUTPUT_HANDLE);
fdOut = _open_osfhandle((intptr_t)consoleHandleOut, _O_TEXT);
fpOut = _fdopen(fdOut, "w" );
*stdout = *fpOut;
setvbuf(stdout, NULL, _IONBF, 0 );
//redirect unbuffered STDERR to the console
consoleHandleError = GetStdHandle(STD_ERROR_HANDLE);
fdError = _open_osfhandle((intptr_t)consoleHandleError, _O_TEXT);
fpError = _fdopen(fdError, "w" );
*stderr = *fpError;
setvbuf(stderr, NULL, _IONBF, 0 );
return TRUE;
}
//Not a console application
return FALSE;
}
//Send the "enter" to the console to release the command prompt on the parent console
static void sendEnterKey(void) {
INPUT ip;
// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
//Send the "Enter" key
ip.ki.wVk = 0x0D; // virtual-key code for the "Enter" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "Enter" key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow) {
int argc = __argc;
char **argv = __argv;
UNREFERENCED_PARAMETER(hInstance);
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(nCmdShow);
BOOL console;
int i;
//Is the program running as console or GUI application
console = attachOutputToConsole();
if (console) {
//Print to stdout
printf("Program running as console application\n");
for (i = 0; i < argc; i++) {
printf("argv[%d] %s\n", i, argv[i]);
}
//Print to stderr
fprintf(stderr, "Output to stderr\n");
}
else {
MessageBox(NULL, "Program running as windows gui application",
"Windows GUI Application", MB_OK | MB_SETFOREGROUND);
}
//Send "enter" to release application from the console
//This is a hack, but if not used the console doesn't know the application has returned
//"enter" only sent if the console window is in focus
if (console && GetConsoleWindow() == GetForegroundWindow()){
sendEnterKey();
}
return 0;
}
The program itself will never know how it was started. Unless you are willing to pass an execution arguments to the program. For example: program.exe -GUI ... you can capture the passed parameters and decide how the program should run based on parameters passed.
your program whould be something like:
class MainClass
{
public static int Main(string[] args)
{
// Test if input arguments were supplied:
if(args[0]=="GUI")
new myGUI().show(); //runs an instance of your gui
else
//you know what should go here
}
}
You can sort of guess whether you are started from the console or not by doing this:
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
fConsole = csbi.dwCursorPosition.X | csbi.dwCursorPosition.Y;
It's a guess -- if your cursor position is not 0,0 than you are in a console and can work as a console app. Otherwise go and create your windows.
Another way to guess is to look at the process tree and see what process launched your app. If it is cmd.exe go in console mode, otherwise go into GUI mode.
Make it a console application and put this into the code:
void ConsoleWindowVisible(bool show)
{
DWORD dummy;
if
(
!show && // Trying to hide
GetConsoleProcessList(&dummy, 1) == 1 // Have our own console window
)
ShowWindow(GetConsoleWindow, SW_HIDE); // Hide the window
else // Trying to show or use parent console window
ShowWindow(GetConsoleWindow, SW_NORMAL); // Show the window
}
int main(int argc, char** argv)
{
ConsoleWindowVisible(false);
}
Cheers.
gor.f.gyolchanyan#gmail.com

How to create virtual keyboard in osx?

I want to create a virtual keyboard in osx. Is it possible? I mean can I make a program that gives same signals than real keyboard. Example of this kind of keyboard would be onscreen keyboard, or keybord viewer (does it have necessary interface btw).
How low should I start? Should I make a device driver e.g. virtual (wireless) keyboard? Or does cocoa etc. have the necessary stuff?
The requirements I have are:
- a list of tuples (time, key_down/key_up, key_code) corresponds to person typing
- virtual keyboard should work side by side with the real one (like touchpad and bluetooh mouse)
- this should work with every program. Hardest examples I can find are: Terminal+vim, remote desktop, games like starcraft
Sample code and links are more than welcome.
edit: The main point is to have programmatic access to keystrokes. There are similar programs but they are with closed source (e.g. http://www.assistiveware.com/keystrokes.php). I want to know what is the best way to make this kind of program.
edit 2: Now I got this party started. Below is a copy-edit-paste-try-something-else code that basically includes all necessary parts to make a virtual keyboard. In this case, every time I press 'a' the virtual keyboard presses 'z'. There is a bug, that there is multiple 'z's added...
#import <ApplicationServices/ApplicationServices.h>
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
UniChar unicodeString[101];
UniCharCount unicharCount;
char chars[2];
int i,j,charsLen;
CGEventRef zDown;
CGEventRef zUp;
zDown = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)6, true);
zUp = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)6, false);
//printf("%u %u\n", (uint32_t)type, (uint32_t) event);
CGEventKeyboardGetUnicodeString(event, 100, &unicharCount, unicodeString);
for (i=0; i < unicharCount; i++)
{
if (unicodeString[i] > 127) {
chars[0] = (unicodeString[i] >> 8) & (1 << 8) - 1;
chars[1] = unicodeString[i] & (1 << 8) - 1;
charsLen = 2;
} else {
charsLen = 1;
chars[0] = unicodeString[i];
}
//for (j = 0; j < charsLen; j++) printf("%c", chars[j]);
}
if (chars[0] == 'a')
{
CGEventPost(kCGHIDEventTap, zDown);
CGEventPost(kCGHIDEventTap, zUp);
}
return event;
}
int main (int argc, const char * argv[]) {
CFMachPortRef eventTap;
CFRunLoopSourceRef runLoopSource;
eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, kCGEventMaskForAllEvents, myCGEventCallback, NULL);
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
return 0;
}
br,
Juha
You could do this using Quartz Event Taps, which provides a:
...C API for event taps, which are
filters used to observe and alter the
stream of low-level user input events
in Mac OS X
On the Mac, there is the keyboard view. Why can't you use that?

Resources