(C++) Disabling Alt commands in Windows - c++

I'm making a program that needs to block all input during a short critical section. I used BlockInput, but it still allows the user to use hotkeys like Ctrl+Alt+F1 or Ctrl+Alt+F2 (switching taskbar in both displays). It is crucial that the user is not able to use these two hotkeys.
I read some things about a hook, but I'm not sure where to start with this solution. Any help would be greatly appreciated.
Thanks!

A keyboard hook could do the trick - check out SetWindowsHookEx. Note that it gets tricky on 64-bit systems.
But may I suggest simply setting your process/thread priority to some ludicrously high value? Windows will really favor your process then, and at the highest settings even keyboard and mouse stopped working - I found that out the hard way. :)

Related

Advice on Buffering keyboard input (C++)

I am trying to use the space bar to pause my game, but don't know how to just toggle my pause bool.
I have a BYTE key[] that I use to check my key state (& 0x80).
I can't seem to find the right way to toggle the key though, ending up in some wonky behavior on my pause (it is a pong clone, so the ball either slows down, pauses, or does nothing).
I would greatly appreciate someone pointing me in the right direction.
EDIT:
I am writing a Win 32 app, using Direct2D.
Key presses are platform specific. You'll need to use platform specific functions to determine when a key is pressed or receive notification that a key was pressed.
In order for StackOverflow users to help you further, you will have to add the platform and compiler version (tools) to your post.
Console programs will have a different process for detecting key presses than windowing programs.

Pressing Win+X, Alt-Tab programmatically

I'm trying to simulate the keypress events for Win+X on Windows 8 which should pop up a small menu, but I have been unable to get this to work by using SendInput. For any other combination of keys (e.g. Win+R, Win+E, Win+D) it works but not for Win+X. I've noticed that Synergy+ has the same problem, but the Windows on-screen keyboard doesn't. I have also looked at the parameters for SendInput that the on-screen keyboard uses but if I use exactly the same parameters in my application I still don't get the menu.
So my question, how do I get this to work? Or is there an alternative way to display this menu?
I've recently added support for this to our application. Glad we beat our competitor to it!
There are new UIPI restrictions in Windows 8. The most-used blocked shortcut is Alt+Tab, so you're going to want to do the workaround.
You have to mark your binaries with uiAccess="true" in the manifest. (For more detail on how to do this, google.) This manifest prevents binaries from being launched unless signed with a Microsoft-approved code signing certificate and installed in a "secure location" (system32 or Program Files/Program Files (x86)).
If you lanch your program from any helpers: The uiAccess binary can't be launched with CreateProcess from a medium integrity process (the manifest marks it as requiring "high" integrity). Instead, it's easiest to launch it using ShellExecute "open" to get the shell to elevate it. If using CreateProcessAsUser, you have to set TokenUIAccess to 1 using SetTokenInformation, or launching will fail.
Final provisos: note that uiAccess quite heavily restricts what a process can do. You can't receive UI input from normal (medium integrity) processes, so other applications can't interact with your windows. If you don't already follow good practices in separating your UI into a separate process, this would therefore be a good reason to do that. Alternatively, the tasks requiring uiAccess could be put into a small, self-contained helper binary and entirely separated from the non-UI process too. Your main app can run it as a high-integrity helper process that is sent instructions as required to perform those specific tasks (such as SendInput).
Finally, SendInput will work.

Block alt+shift event or disable changing language in programming way

I need to block alt+shift keys event using C++, or some way to block changing language.
Thanks in advance.
I would go about it a bit differently. I would catch current langauge settings at startup than change it for desired one.
At the even of alt+shift i would just set it back to the desired type again.
It should be fairy easy to do with net framework.
Here is a short article about manipulating languages: How to change input language programmatically
And main class on msdna: InputLanguage documentation
To actually prevent alt+shift from moving into windows system you would have to play with hooks.
Here is an article about blocking keystrokes before windows process them.
Just an idea. You can catch WM_KEYDOWN message and call ActivateKeyboardLayout to switch language.
Using C++ you can install a keyboard hook procedure like the one suggested here and filter (swallow/don't propagate) the key(s) you want to forbid.
My understanding of MSDN is that you can pretend to process WM_INPUTLANGCHANGEREQUEST and then do nothing, so that Windows will not do anything further and the language will not actually change. But some users say that doesn't work any more.
http://msdn.microsoft.com/en-us/library/ms632630(VS.85).aspx
Maybe you can implement ITfInputProcessorProfileActivationSink::OnActivated, and when you get called you can change back to the previous language by calling ITfInputProcessorProfiles::ActivateLanguageProfile. At the beginning of your app you would call ITfInputProcessorProfiles::GetActiveLanguageProfile.
Maybe you can implement ITfLanguageProfileNotifySink::OnLanguageChange, set *pfAccept
to FALSE and return S_OK.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms628770(v=vs.85).aspx
All of the above have a problem. If the user intentionally changes languages on the client, for example by clicking on the client's task bar instead of pressing Alt+Shift, the above methods will prevent their change anyway.
I wonder if RegisterHotKey would let you register Alt+Shift for your own window even though the system already had it defined.
The thing you are trying to implement is keyboard hook. The detailed explanation with source code in C/C++ can be found here:
http://www.codeproject.com/Articles/67091/Mouse-and-KeyBoard-Hooking-utility-with-VC
Also other helpful examples can be found here:
http://www.codeproject.com/Articles/1264/KeyBoard-Hooks
http://www.codeproject.com/Articles/9513/Disable-keyboard-and-show-images-for-the-children
Hope this helps.
Kind regards,
Bo

Grabbing events on specific keys with X11 on Linux

I'm writing a program in C++ to implement the keyboard backlight feature from OS X on MacBook Pro's running a Linux distro. So far, it turns the backlight on, on boot and if no keyboard and mouse events are registered for 20 seconds, it will turn it back off, and of course turn it on yet again when an event is registered. Next thing I need the program to do, is to capture keypresses on the keyboard-backlight-up/down keys, but I'm not sure how to approach this.
I am currently using XScreenSaverQueryInfo to get the idle time of keyboard and mouse events, so a method using X11 API would be okay. I have done a lot of googling but havent found a way that I felt sure about going with. The problem I'm seeing with lots of the methods I found, is that they use keycode to identify the key, but I dont think that would be a viable solution since the program should work for any keyboard-layout available.
Any idea of a method and API I should go with? What would work the best?
Regards,
The normal way to do this is with XGrabKey(). It uses keycodes, but you wouldn't hardcode the keycode, you'd get it with XKeysymToKeycode(). To be more correct you'd also want to redo the grab when you get a MappingNotify (XMappingEvent). (Note, MappingNotify, not MapNotify.) If there isn't a keysym for these keys - there probably isn't on old X versions, but hopefully newer X.org versions have one - then you just have to hardwire the keycode. Which won't be very robust or portable but probably works for everyone on Linux with the same hardware model.
Be prepared that key grabs are global, so if you try to XGrabKey() and something else has already grabbed that key, you'll get an X error - by default that exits the program. Another quirk of XGrabKey() is that it grabs the key with a precise modifier set. For example, to handle both with and without NumLock, you need to grab twice. See Global Hotkey with X11/Xlib
In a normal Linux setup (if you wanted to get a feature like this into upstream projects), the desktop environments don't want lots of separate apps fighting over the key grabs and getting errors. So there will be some central coordination points, such as the window manager or a special daemon might do all the keybindings and forward commands to other processes as needed. So you would probably want to look at patching the same upstream code that handles other special keys like this, if you were trying to get your feature integrated into distributions by default.
Another thing to be aware of is the Xkb API, which is a lot more complicated. There is some brain-bending way to grab keys with Xkb but I don't know of any advantage to going that route.
If you haven't done that yet, familiarize yourself with xev. Start it, give it the focus, and press the keys, to see what's happening.

Parse information from programs added to taskbar with C++

Basically what I am trying to do is write my own pseudo task bar in C++. The program needs to idle until another program is started up, at which point it needs to visually depict that the other program is running. For each other program that is running, the user should be able to click on the visual representation and have Windows switch focus to the selected program.
The big underlying question at this point: is this even a possibility? Or has Windows hidden most/all of its fiddly-bits to make this close to, if not completely, impossible?
[EDIT:] restructured the question
The obvious starting point would be SetWindowsHookEx(WH_SHELL,...); which will get you notifications when top-level windows are created or destroyed (along with some other related events, like a different window being activated, a window's title changing, etc.)
Think ahead to actually bringing the window to the front, as I once researched myself.
SetForegroundWindow() won't work unless issued from the foreground process - neither SwitchToThisWindow() nor the AttachThreadInput() kludge seemed to always work, but maybe I just wasn't doing it right. Anyway as far as I know there no way to make a window foreground as good as Windows does, please enlighten me if say you discover say an undocumented call which actually Works.
It seems possible to me at least in a basic way:
1. Set up a shell hook as described by Jerry
2. figure the executable file from the module handle to access it's icons using shell services
The Vista-like feature of keeping a 'live' miniature of the screen seems much more challenging.