The key point of a map is that no two elements in a map container can have equivalent keys. If the number of significant digits entered is less than the precision of double values, this will not happen (besides entering the same value again which applies to ints as well).
But you might get problems when trying to access elements by key when the argument is from other sources (affected by rounding).
That would be my bad feeling:
While there is no problem with the map itself, key based operations (operator, erase, swap, find) might behave not as expected (no match). Even when not using such operations now but only iterator based ones, they may be used later when updating the code. So you should at least place a comment about this.
Another option is creating your own std::map based class that disables the affected operations or replaces them with a version that treats close numbers as identical (defining an epsilon).
I was avoiding this discussion but I wanted to just add one more comment. The FPU has a state that has an affect on the precision. In other words... when the FPU is in various states you get a different rounding value. You can control this state with _controlfp_s
Printer DLL and other libraries outside of your control could potentially change the thread FPU state causing your std::map to contain multiple floating point numbers from the same calculations. Unfortunately on Windows user-applications cannot always control third-party DLL from loading into your process. Those DLL could modify and then fail to reset the floating point state.
The answer is "Yes" what you are proposing would work 99% of the time or in "laboratory" conditions where you control both the hardware and software. But if you tried to ship out a commercial product using the above technique... you will almost certainly have a small percentage of the conditions I described above.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
What is the recommended way to listen for a key press globally? At first, adding an infinite loop came to mind (for(;;), or while(true)), but these are bad choices, as they eat a lot of CPU, and adding sleep will cause to lose some of the key press events.
I want to make it to check any key (or key combination), to see what is pressed on Windows OS.
It depends if "globally" applies to the system (user session) or an application. For an application it depends on the type (console or GUI). With GUI applications it depends also if you want to catch all / multiple or only a single / few key combinations.
This function will be called from Java, which app will be minimized, or maybe running in the background. The app won't be focused, so yes, I need it for the entire user session, even if another application is focused, I want it to still register the key press and do something in case a key I randomly choose is pressed. I will make some profiles: profile 1 has key "A", profile 2 has key "CTRL + A", those combinations will be changeable.
For what I have now, I'll use only one key, and I used "SetWindowsHookEx". Is it possible to use that for a combination of keys? I don't need it in this application, but in future I will need it. All the examples I saw on google were with only 1 key, or using the mouse. If you can use it for key combination, can it be used to combine 4-5 keys? Yes, it is hard to press that many at a time, but you can map an extra key from the keyboard to press all of them at the same time.
With key combinations I meant a combination of a single key while also Shift, Ctrl, and or Alt are down.
A combination of multiple keystrokes makes no sense because that can't be "eaten" (the initial strokes has to be passed and will be processed by the window having the focus).
It is still unclear what you finally want to do. If is not a personal project, be very careful what you are doing. Such global "features" might interfere with the active application which is a "don't do it".
So, let me reformulate my last statement.
What is the alternative of SetWindowsHookEx, for a combination of keys, by checking if Ctrl and/or Alt and/or Shift is down + any other key (different from Windows combinations)?
For a single button (keyboard or mouse) it is great, but from what I've seen, it can't be used to check at the same time if any Ctrl, Alt, Shift is down.
I have registered USB serial devices for notification and get a device arrival & removal notice now in OnDeviceChange. However, always when I try to print out the dbcc_name, its length is one wide character (as defined in the structure documentation) and nothing more prints out. I have browsed countless examples where the code proceeds to extract for instance a VID/PID from the string. What am I doing wrong? The examples also give me one character. I am using windows 10 & VS2017.
I have even reserved a larger buffer for notification registration to fit in the full dbcc_name string. Nothing helps, just get that one character. I have also tried the WideCharToMultibyte and then instead of a Chinese character I get a single question mark. Please advise.