How to detect if a key physically exists on a keyboard - c++

I am working on a text editor and I use it on both my desktop and laptop. My desktop has some keys that my laptop does not, and they are used in some bindings. I want to be able to do something like this in my config file
if keyDoesNotExist(KEY_HOME) then
bind(bla, bla, bla)
end
So is it possible to detect if keys are present on the current keyboard?
This looks like it might have what I'm looking for. It mentions returning the device handle to the physical keyboard layout but does not elaborate further.

Related

Qt: How to Write a Windows Desktop Utility?

I've used Qt for widget development a fair bit. That's straightforward. But how could I write a program that can be invoked from anywhere on Windows?
Say I've selected some text in a web browser. I want to be able to invoke my program via keystroke, maybe show a little list containing a list of text files I can select (ideally where the cursor is), and then add the selected text to that file.
How can I do this?
I suggest you to have a look at:
https://skycoder42.github.io/QtService/index.html
https://forum.qt.io/topic/67043/qt-daemon

What function to use to select text?

I search a lot of on internet but didn't find some good copy-paste manager for windows 7. I want to make something to easy copying multiple texts. So my question is what function to use in c++ to select some text that will be copy. The plan is that every time when is pressed CTRL+C selected text copy to some txt file, and when is pressed CTRL+V application show you what is in that file, and you can use what text you need. The main question is how to select text, what function to use? Platform win 7.
You should read up a bit on how the Windows clip board works. Every application in the system can place objects of different formats (including text) on the clip board. The easiest way to grab the content out of any applications is probably to somehow monitor the clip board and get the data from there.
For the pasting part, if I remember correctly, there is a special kind of "owner-handled" data on the clip board. Using that, the data isn't actually published on the clip board, only a reference to the application currently having the clip board data. Whenever the data is pasted the application gets notified that it should send the data to the recipient. It should be possible to exploit that functionality to get your application to pop up a windows where the user can select what data to paste.
Please see my articles on clipboard viewer implementation, including common pitfalls:
http://www.clipboardextender.com/developing-clipboard-aware-programs-for-windows/6
http://www.clipboardextender.com/developing-clipboard-aware-programs-for-windows/common-general-clipboard-mistakes

Setting the label for a Windows networking mapping

Is it possible to give a network drive mapping (as created with the WNetAddConnection functions or "Map network drive..." GUI) a label other than the default "<Target Name> (<Target Path>) (<Drive Letter>:)" one?
I tried giving SetVolumeLabel a go but this always fails, and I see nothing in the WNet API's to specficy the display label.
This isn't a 100% solution but it's more of an answer than a comment...
If you rename a mapped network drive the the GUI (by right clicking on it and going to 'Rename') it adds a value to the registry. Reading round on various sites (notably this one) it looks like Windows may sporadically delete this value by itself, so this may not be a permanent solution...
I have just manually done it through regedit and it worked in the GUI, so I see no reason why it shouldn't work programmatically as well
Add a string value called _LabelFromReg with a value of whatever you want the label to be to the registry key
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##<server-name>#<share-name>
This key should already exist if you have already created the share.
Apparently (see the link above) you then need to make that key read-only to prevent the OS from changing it back at will - I don't know how you would do that programmatically but i'm sure it can be done.
I know there are huge gaps in this answer, but maybe it's a poke in the right direction?

How to assign keys on physical position at the keyboard on Windows?

There are at least 3 different keyboard layouts who are using the program i make, and i dont care which key they press, the more important thing is where the keys are pressed.
I cant just ask the user to press 100 different keys/combinations, that would take too much time and be very confusing to the user.
Is there some library or something that can do this for me?
This answer says that you can use the keyboard "Scan Code" which should be the same on all keyboards. So it appears that there is in fact a way to determine the key location.
Even without using the scan code, you could implement a mapping table for all the keyboard layouts you support. So if the keyboard layout is US English, the key would map to command A, but on layout German German, it would map to command B, and some other command would map to command A. It may not work for all keyboards, but if it works for all of your users that's what matters. I can see why you were looking for an existing library to do this as it would be a significant amount of work.
This cannot be done. Keyboards do not come with their layout in an electronic form, that could be looked up by the OS. They just send the code for the key pressed, not where the key resides. I'm working on several keyboards daily, which are all classified as having German layout, but with some of the keys being in different places.
The only way to find out at what physical position a specific key is on a specific keyboard is to look at what's printed on the key.

Keyboard input in games (for GLUT)

Almost every game use keyboard as input. I have been searching for 2 days on this topic and found quite much about it. Keyboards have many disadvantages, but main problems I found are different layouts and second that if you are pressing 3 keys at time, it can lead to corruption (row-column error). If you don't know what I'm talking about, keyboard is made as grid and it checks which row and column has connected. But if you press E,D (row 1,2 column 3)
and R (row 1, column 4), keyboard can show even F because it find it pressed(row 2, column 4 both pressed).
So I think we can't do anything about that second, but if anyone got idea how to solve it better then use keys that don't form L, I'd be glad :)
But my main problem are different keyboard layouts which is real pain. I'm slovak so Slovak layout of numbers look like this:
+ľščťžýáíé and with shift 1234567890, we also got QWERTZ but you can use QWERTY.
You all know how the English look like but just for sure:
1234567890 and shift !##$%^&*()
Most of time I'm using english one because I got used to it when programming. Anyway there are different people using different layouts. When you are making game that depend on which key is pressed, for example good old WASD pattern, you can't use that on french one which is AZERTY layout. It would be strange. Same as using numbers for choosing gun in action game. As you can see slovak would have to press shift to get it work.
I'm also using OpeGL. There is problem when you are mapping which keys are pressed. For example widely used solution to make map of 256 bools for each charakter, is suffering from SHIFT. You press a, SHIFT and release a you got: a down, A up. So I thought about binding some keys together, as A and a, 1 and !, but then I realized I'll just change layout and everything is wrong.
So what is solution for that? I think there is someone out there that is in game industry or made some game and had to solve this. Only solution that comes on my mind is to force english layout for UI (and choosen layout for chat).
After next searching I found what I need but I need cross-platform one:
virtual key codes
And next search revealed SDL key
Result: Don't ever start with GLUT if you go making games, use SFML or SDL
Thanks to everyone for helping me, there were more problems in this so idea of key binding/mapping, SDL and so on, each helped me alot.
If you're getting a "character" every time user presses something, then your keyboard routines aren't suitable for game input - only for text entry.
You need to use input routines that completely ignore keyboard layout switching and operate on some kind of raw keycodes (so when user presses shift+a, you'll know that shift is pressed and that "a" key is pressed, but you won't get "A" character). Also, routines should allow you to query if a key is pressed or not.
For windows that is DirectInput or XInput. For cross-platform this is libsdl and SDL_GetKeyState. And you'll need to provide keymapping options for the user. Glut probably isn't suitable for your task.
The common approach seems to be to ignore the problem. Worse-is-better in its early stage.
Unfortunately I'm using svorak keyboard layout so it really doesn't just work for me.
I've been approaching this same problem by binding into multiple keys on the keyboard. So that player jumps from both x and j -keys. It doesn't do so well in something that isn't shoot-jump -kind of game.
Nice stuff would be if you could just find row/col or some driver-near interface to your keyboard.
Some auto-keyboard configuration software would be neat though I've not yet seen anything like that. Maybe we should write one?
First up, separate keypresses from text entry. You shouldn't care what letter or number comes up when you press a key with shift as well - the operating system should handle that and generate an event you can use in the rare times you need the text. Usually, you should just look for the key press and any shift presses and act on those.
Second, load the bindings from keys to commands from a data file, rather than hardcoding it. Distribute default bindings for QWERTY and whatever default layout you have. If the data format is quite straightforward then people won't mind customising it to fit their keyboard and preferences. You can also add an in-game keybinding editor later.
This isn't really about OpenGL since by default that doesn't care about keypresses. Perhaps you are using an addon library or extension that handles keys for you - ensure that whatever you're using can give you individual key values and the state of shift/alt/ctrl independently, and that it also provides text input via an independent system.
Allow your users to define the keys to use for each action ...
or use the arrow keys .. that should be pretty universal :)
Turn the keyboard input into metadata, so you could allow users to configure at their will but also provide different keyboard shortcuts depending on the keyboard layout used in a config file .