WM_CHAR and strange characters with Alt-key - winapi

I have an MFC CWnd derived text editor and when I (accidentally) press Alt+A, WM_CHAR inserts a character '☺'.
Spy++ logs (I can't copy/paste - I had to type this):
WM_SYSKEYDOWN nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:0 AltDown:1 fRepeat:0 fUp:0
WM_SYSKEYDOWN nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:0 AltDown:1 fRepeat:1 fUp:0
WM_SYSKEYDOWN nVirtKey:'A' cRepeat:1 ScanCode:1 fExtended:0 AltDown:1 fRepeat:0 fUp:0
WM_SYSCHAR chCharCode:97 cRepeat:1 ScanCode:1E fExtended:0 AltDown:1 fRepeat:0 fUp:0
WM_SYSKEYUP nVirtKey:'A' cRepeat:1 ScanCode:1E fExtended:0 AltDown:1 fRepeat:1 fUp:1
WM_KEYUP nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:0 AltDown:0 fRepeat:1 fUp:1
WM_CHAR chCharCode:63 cRepeat:1 ScanCode:38 fExtended:0 AltDown:1 fRepeat:1 fUp:1
My debugger says OnChar() gets nChar = 0x0000263a, nFlags = 0x0000c038, nRepCnt=1.
How can I distinguish this code from a proper code like AltGr+m = µ on a German keyboard?

Related

How can I detect these x coordinates?

The blue points are a scatterplot (x-y points), not "lines"
The question is, how can I detect the x coordinates at which the "lines" end (marked with green arrows)
I think that the coordinates are proportional to powers of 2 (here is the same chart in logarithmic scale)
The red line shows the accumulated number of points. There is not a fall on the point density where the lines end.
The "lines" do not end all at the same x coordinate, and the y coordinates depend on the dataset (not all are at the same scales, so I cannot just hardcode one y coordinate)
Maybe if I could detect the periodicity of the y coordinate, I could group the points by their approximate maximum x value. (note that the minimum y separation is also unknown)
I'm not asking about a particular language but a suggestion for an algorithm. (although I prefer R or python)
I don't even know what tag to use for this question, I welcome comments on the appropriate tags.
Here is a sample file, and here is a snippet from it:
0.000035048,-0.000012875
0.000035107,0.000000060
0.000039995,0.000004888
0.000036120,-0.000003874
0.000036716,0.000000596
0.000037014,0.000000298
0.000038803,0.000001788
0.000039697,0.000000894
0.000039995,0.000000298
0.000041783,0.000001788
0.000039101,-0.000002682
0.000041485,0.000002384
0.000041008,-0.000000477
0.000039995,-0.000001013
0.000040054,0.000000060
0.000040889,0.000000834
0.000041485,0.000000596
0.000044882,0.000003397
0.000044882,0.000000000
0.000047863,0.000002980
0.000045002,-0.000002861
0.000045002,0.000000000
0.000047684,0.000002682
0.000047326,-0.000000358
0.000047207,-0.000000119
0.000047207,0.000000000
0.000047386,0.000000179
0.000047386,0.000000000
0.000047445,0.000000060
0.000047982,0.000000536
0.000048995,0.000001013
0.000050008,0.000001013
0.000053227,0.000003219
0.000054479,0.000001252
0.000054777,0.000000298
0.000053108,-0.000001669
0.000050008,-0.000003099
0.000051796,0.000001788
0.000051618,-0.000000179
0.000052810,0.000001192
0.000052810,0.000000000
0.000051975,-0.000000834
0.000051558,-0.000000417
0.000053465,0.000001907
0.000053406,-0.000000060
0.000053406,0.000000000
0.000053406,0.000000000
0.000052810,-0.000000596
0.000052810,0.000000000
0.000052571,-0.000000238
0.000052571,0.000000000
0.000051975,-0.000000596
0.000050008,-0.000001967
0.000048280,-0.000001729
0.000047445,-0.000000834
0.000047445,0.000000000
0.000047445,0.000000000
0.000047445,0.000000000
0.000049531,0.000002086
0.000049353,-0.000000179
0.000049412,0.000000060
0.000049412,0.000000000
0.000048876,-0.000000536
0.000048518,-0.000000358
0.000047982,-0.000000536
0.000047803,-0.000000179
0.000047803,0.000000000
0.000048935,0.000001132
0.000047743,-0.000001192
0.000047624,-0.000000119
0.000047445,-0.000000179
0.000047147,-0.000000298
0.000046015,-0.000001132
0.000044882,-0.000001132
0.000043213,-0.000001669
0.000039995,-0.000003219
0.000039995,0.000000000
0.000039279,-0.000000715
0.000039995,0.000000715
0.000039995,0.000000000
0.000041008,0.000001013
0.000041068,0.000000060
0.000044882,0.000003815
0.000041544,-0.000003338
0.000041544,0.000000000
0.000043392,0.000001848
0.000042021,-0.000001371
0.000042021,0.000000000
0.000043929,0.000001907
0.000044763,0.000000834
0.000042200,-0.000002563
0.000044465,0.000002265
0.000044465,0.000000000
0.000044465,0.000000000
0.000043511,-0.000000954
0.000043988,0.000000477
0.000043929,-0.000000060
0.000043929,0.000000000
0.000043929,0.000000000
0.000043929,0.000000000
0.000043869,-0.000000060
0.000043809,-0.000000060
0.000043809,0.000000000
0.000045300,0.000001490
0.000045300,0.000000000
0.000045717,0.000000417
0.000045478,-0.000000238
0.000045478,0.000000000
0.000045478,0.000000000
0.000044763,-0.000000715
0.000044763,0.000000000
0.000044465,-0.000000298
0.000042796,-0.000001669
0.000042856,0.000000060
0.000042856,0.000000000
0.000044405,0.000001550
0.000044286,-0.000000119
0.000044286,0.000000000
0.000044286,0.000000000
0.000044286,0.000000000
0.000044405,0.000000119
0.000044465,0.000000060
0.000045478,0.000001013
0.000045478,0.000000000
0.000045419,-0.000000060
0.000045002,-0.000000417
0.000043631,-0.000001371
0.000043631,0.000000000
0.000044882,0.000001252
0.000044882,0.000000000
0.000044823,-0.000000060
0.000044703,-0.000000119
0.000044644,-0.000000060
0.000044644,0.000000000
0.000048399,0.000003755
0.000048161,-0.000000238
0.000048101,-0.000000060
0.000048161,0.000000060
0.000048161,0.000000000
0.000048161,0.000000000
0.000048459,0.000000298
0.000048459,0.000000000
0.000048459,0.000000000
0.000048280,-0.000000179
0.000048280,0.000000000
0.000048459,0.000000179
0.000048280,-0.000000179
0.000047982,-0.000000298
0.000048280,0.000000298
0.000048220,-0.000000060
0.000047982,-0.000000238
0.000047982,0.000000000
0.000047982,0.000000000
0.000048399,0.000000417
0.000048399,0.000000000
0.000048459,0.000000060
0.000048399,-0.000000060
0.000048399,0.000000000
0.000046253,-0.000002146
0.000046194,-0.000000060
0.000047982,0.000001788
0.000048161,0.000000179
0.000048161,0.000000000
0.000048161,0.000000000
0.000047982,-0.000000179
0.000047982,0.000000000
0.000047982,0.000000000
0.000047982,0.000000000
0.000047088,-0.000000894
0.000047982,0.000000894
0.000047982,0.000000000
0.000047982,0.000000000
0.000047982,0.000000000
0.000047982,0.000000000
0.000047982,0.000000000
0.000047266,-0.000000715
0.000047982,0.000000715
0.000047982,0.000000000
0.000047982,0.000000000
0.000047982,0.000000000
0.000047982,0.000000000
0.000047982,0.000000000
0.000048399,0.000000417
0.000048399,0.000000000
0.000048399,0.000000000
0.000048399,0.000000000
0.000047982,-0.000000417
0.000047982,0.000000000
0.000047266,-0.000000715
0.000047982,0.000000715
0.000047982,0.000000000
0.000047982,0.000000000
0.000047982,0.000000000
0.000047505,-0.000000477
0.000047505,0.000000000
0.000047982,0.000000477
0.000047982,0.000000000
0.000047982,0.000000000
0.000048339,0.000000358
0.000048339,0.000000000
0.000048399,0.000000060
0.000047803,-0.000000596
0.000048280,0.000000477
0.000048280,0.000000000
0.000048280,0.000000000
0.000048280,0.000000000
0.000048280,0.000000000
0.000048280,0.000000000
0.000048161,-0.000000119
0.000048161,0.000000000
0.000048101,-0.000000060
0.000048101,0.000000000
0.000047922,-0.000000179
0.000047863,-0.000000060
0.000047028,-0.000000834
0.000047028,0.000000000
0.000047982,0.000000954
0.000047982,0.000000000
0.000047982,0.000000000
0.000047982,0.000000000
0.000047982,0.000000000
0.000047982,0.000000000
0.000047982,0.000000000
0.000047088,-0.000000894
0.000047982,0.000000894
0.000047982,0.000000000
0.000047505,-0.000000477
0.000047922,0.000000417
0.000047505,-0.000000417
0.000047207,-0.000000298
0.000047147,-0.000000060
0.000047207,0.000000060
0.000047088,-0.000000119
0.000047088,0.000000000
0.000047028,-0.000000060
0.000047028,0.000000000
0.000047028,0.000000000
0.000047028,0.000000000
0.000047803,0.000000775
0.000047803,0.000000000
0.000047803,0.000000000
0.000047803,0.000000000
0.000047803,0.000000000
0.000047803,0.000000000
0.000047922,0.000000119
0.000047863,-0.000000060
0.000047863,0.000000000
0.000047863,0.000000000
0.000047922,0.000000060
0.000047922,0.000000000
0.000047922,0.000000000
0.000047863,-0.000000060
0.000047863,0.000000000
0.000047028,-0.000000834
0.000046611,-0.000000417
0.000046611,0.000000000
0.000046611,0.000000000
0.000046611,0.000000000
0.000046611,0.000000000
0.000046611,0.000000000
0.000046015,-0.000000596
0.000046015,0.000000000
0.000046015,0.000000000
0.000046015,0.000000000
0.000046015,0.000000000
0.000046015,0.000000000
0.000046015,0.000000000
0.000046015,0.000000000
0.000046015,0.000000000
0.000046015,0.000000000
0.000046015,0.000000000
0.000045002,-0.000001013
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000043869,-0.000001132
0.000043690,-0.000000179
0.000043690,0.000000000
0.000045776,0.000002086
0.000045776,0.000000000
0.000045776,0.000000000
0.000045776,0.000000000
0.000043690,-0.000002086
0.000043690,0.000000000
0.000043690,0.000000000
0.000043750,0.000000060
0.000043750,0.000000000
0.000043690,-0.000000060
0.000045776,0.000002086
0.000045776,0.000000000
0.000045776,0.000000000
0.000045776,0.000000000
0.000045776,0.000000000
0.000045776,0.000000000
0.000045776,0.000000000
0.000045717,-0.000000060
0.000045717,0.000000000
0.000045717,0.000000000
0.000044107,-0.000001609
0.000044107,0.000000000
0.000044107,0.000000000
0.000044107,0.000000000
0.000044107,0.000000000
0.000045776,0.000001669
0.000046015,0.000000238
0.000046015,0.000000000
0.000046015,0.000000000
0.000046015,0.000000000
0.000046015,0.000000000
0.000046015,0.000000000
0.000046015,0.000000000
0.000045002,-0.000001013
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000044167,-0.000000834
0.000044167,0.000000000
0.000044167,0.000000000
0.000045300,0.000001132
0.000045300,0.000000000
0.000045300,0.000000000
0.000046790,0.000001490
0.000046790,0.000000000
0.000046790,0.000000000
0.000046790,0.000000000
0.000046790,0.000000000
0.000046790,0.000000000
0.000046790,0.000000000
0.000046790,0.000000000
0.000046790,0.000000000
0.000046790,0.000000000
0.000046790,0.000000000
0.000046790,0.000000000
0.000046790,0.000000000
0.000046730,-0.000000060
0.000046730,0.000000000
0.000046730,0.000000000
0.000046730,0.000000000
0.000046730,0.000000000
0.000046730,0.000000000
0.000046730,0.000000000
0.000046730,0.000000000
0.000046730,0.000000000
0.000046730,0.000000000
0.000046730,0.000000000
0.000046730,0.000000000
0.000045121,-0.000001609
0.000045121,0.000000000
0.000045121,0.000000000
0.000045121,0.000000000
0.000045121,0.000000000
0.000045002,-0.000000119
0.000045002,0.000000000
0.000045002,0.000000000
0.000044644,-0.000000358
0.000043750,-0.000000894
0.000042975,-0.000000775
0.000042796,-0.000000179
0.000042796,0.000000000
0.000042796,0.000000000
0.000042140,-0.000000656
0.000042140,0.000000000
0.000042140,0.000000000
0.000042200,0.000000060
0.000042200,0.000000000
0.000045002,0.000002801
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000042379,-0.000002623
0.000042379,0.000000000
0.000044882,0.000002503
0.000044882,0.000000000
0.000044882,0.000000000
0.000044823,-0.000000060
0.000044823,0.000000000
0.000044823,0.000000000
0.000044823,0.000000000
0.000044823,0.000000000
0.000044823,0.000000000
0.000044882,0.000000060
0.000044882,0.000000000
0.000044882,0.000000000
0.000044823,-0.000000060
0.000044823,0.000000000
0.000044823,0.000000000
0.000044823,0.000000000
0.000044823,0.000000000
0.000044823,0.000000000
0.000044823,0.000000000
0.000044823,0.000000000
0.000044823,0.000000000
0.000044882,0.000000060
0.000042677,-0.000002205
0.000042737,0.000000060
0.000044763,0.000002027
0.000044763,0.000000000
0.000044763,0.000000000
0.000043511,-0.000001252
0.000043511,0.000000000
0.000044763,0.000001252
0.000044763,0.000000000
0.000044763,0.000000000
0.000044763,0.000000000
0.000044763,0.000000000
0.000044763,0.000000000
0.000044763,0.000000000
0.000044763,0.000000000
0.000044763,0.000000000
0.000044882,0.000000119
0.000044882,0.000000000
0.000044882,0.000000000
0.000044882,0.000000000
0.000044882,0.000000000
0.000044882,0.000000000
0.000044882,0.000000000
0.000044882,0.000000000
0.000045002,0.000000119
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000046492,0.000001490
0.000046492,0.000000000
0.000047028,0.000000536
0.000047505,0.000000477
0.000047803,0.000000298
0.000047863,0.000000060
0.000047922,0.000000060
0.000047803,-0.000000119
0.000047803,0.000000000
0.000047803,0.000000000
0.000047803,0.000000000
0.000047922,0.000000119
0.000047982,0.000000060
0.000047982,0.000000000
0.000047982,0.000000000
0.000047982,0.000000000
0.000047982,0.000000000
0.000047803,-0.000000179
0.000047803,0.000000000
0.000047803,0.000000000
0.000047803,0.000000000
0.000047803,0.000000000
0.000045359,-0.000002444
0.000045359,0.000000000
0.000045300,-0.000000060
0.000045359,0.000000060
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000044525,-0.000000834
0.000044525,0.000000000
0.000044525,0.000000000
0.000044525,0.000000000
0.000044525,0.000000000
0.000044525,0.000000000
0.000044525,0.000000000
0.000044525,0.000000000
0.000045359,0.000000834
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000043988,-0.000001371
0.000043988,0.000000000
0.000043988,0.000000000
0.000045359,0.000001371
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000044048,-0.000001311
0.000045359,0.000001311
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000045359,0.000000000
0.000044525,-0.000000834
0.000045121,0.000000596
0.000045121,0.000000000
0.000045121,0.000000000
0.000045121,0.000000000
0.000045121,0.000000000
0.000045121,0.000000000
0.000044048,-0.000001073
0.000043988,-0.000000060
0.000043750,-0.000000238
0.000043750,0.000000000
0.000043750,0.000000000
0.000043750,0.000000000
0.000043750,0.000000000
0.000043750,0.000000000
0.000043750,0.000000000
0.000043750,0.000000000
0.000043809,0.000000060
0.000044882,0.000001073
0.000044882,0.000000000
0.000044882,0.000000000
0.000044882,0.000000000
0.000043809,-0.000001073
0.000043750,-0.000000060
0.000043750,0.000000000
0.000043750,0.000000000
0.000043750,0.000000000
0.000043750,0.000000000
0.000043750,0.000000000
0.000043750,0.000000000
0.000043750,0.000000000
0.000043750,0.000000000
0.000043631,-0.000000119
0.000043631,0.000000000
0.000043631,0.000000000
0.000043571,-0.000000060
0.000043631,0.000000060
0.000043631,0.000000000
0.000045359,0.000001729
0.000047028,0.000001669
0.000047028,0.000000000
0.000047028,0.000000000
0.000047028,0.000000000
0.000043988,-0.000003040
0.000045955,0.000001967
0.000045955,0.000000000
0.000045955,0.000000000
0.000045955,0.000000000
0.000045955,0.000000000
0.000045955,0.000000000
0.000045002,-0.000000954
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000045002,0.000000000
0.000044107,-0.000000894
0.000044107,0.000000000
0.000044107,0.000000000
0.000044107,0.000000000
0.000044107,0.000000000
0.000043690,-0.000000417
0.000043690,0.000000000
0.000043690,0.000000000
0.000043690,0.000000000
0.000043690,0.000000000
0.000043690,0.000000000
0.000043690,0.000000000
0.000043690,0.000000000
0.000043690,0.000000000
0.000043988,0.000000298
0.000043988,0.000000000
0.000043988,0.000000000
0.000043988,0.000000000
0.000043988,0.000000000
0.000043988,0.000000000
0.000043988,0.000000000
0.000043988,0.000000000
0.000043988,0.000000000
0.000043988,0.000000000
0.000043988,0.000000000
0.000043988,0.000000000
0.000043988,0.000000000
0.000043988,0.000000000
0.000043988,0.000000000
0.000043988,0.000000000
0.000043988,0.000000000
I had a go on analysing the numbers, starting with this Python script:
file = open(r"C:\Users\timok\Downloads\sample_.csv")
d = {}
for line in file:
x, y = map(float, line.split(","))
d[y] = max(x, d.get(y, x))
for y, x in sorted(d.items()):
print(y, x)
This aggregates the data by Y-coordinate, keeping the maximum X-coordinate. This seemed already a good push in the right direction.
Looking at these results, I could see that these coordinates were multiples of a common (small) factor. After playing around, I found that factor to be near 1/16777215, so that allowed me to map the coordinates to integers:
d = {}
c = 16777215
for line in file:
x, y = map(float, line.split(","))
y = round(y*c)
x = round(x*c)
d[y] = max(x, d.get(y, x))
for y, x in sorted(d.items()):
print(y, x)
Looking at the Y-values around 0, the output is:
...
1 2044
2 4096
3 2050
4 8192
5 2052
6 4100
7 2039
8 16384
9 1991
10 4104
11 2041
12 8200
13 1896
14 4108
15 2011
16 32768
...
And here we see the pattern arise: where Y is a power of 2, the corresponding maximum X coordinate is also a power of 2 and also doubling each time. We see a little noise on the first value (2044 instead of 2048), but the next few values are powers of 2:
...
1 2044
2 4096
4 8192
8 16384
16 32768
...
Looking further, we see this pattern:
The (mapped) X-coordinate is 211+z, where z represents the number of trailing zeroes that the (mapped) Y-coordinate has in its binary representation. So:
y
binary
z
x = 211+z
1
00001
0
2048
2
00010
1
4096
3
00011
0
2048
4
00100
2
8192
5
00101
0
2048
6
00110
1
4096
7
00111
0
2048
8
01000
3
16384
9
01001
0
2048
10
01010
1
4096
11
01011
0
2048
12
01100
2
8192
13
01101
0
2048
14
01110
1
4096
15
01111
0
2048
16
10000
4
32768
So the boundary values you were looking for are 211+z for any natural number z. But this is after we did the mapping. So without the mapping, we get:
211+z / 16777215

How press "shift" + "command" + "3" simultaneously programmatically

I am trying to capture a screenshot by posting this Keyboard event. I tried below way which doesn't work:
1]
CGEventRef event1, event2, event3, event4, event5, event6;
event1 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, true);
event2 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)55, true);
event3 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)85, true);//or 20
event4 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)85, false);// or 20
event5 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)55, false);
event6 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, false);
CGEventPost(kCGSessionEventTap, event1);
CGEventPost(kCGSessionEventTap, event2);
CGEventPost(kCGSessionEventTap, event3);
CGEventPost(kCGSessionEventTap, event4);
CGEventPost(kCGSessionEventTap, event5);
CGEventPost(kCGSessionEventTap, event6);
CFRelease(event1);
CFRelease(event2);
CFRelease(event3);
CFRelease(event4);
CFRelease(event5);
CFRelease(event6);
2]
CGEventRef event7;
event7 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)85, true);//or 20
CGEventSetFlags(event7, kCGEventFlagMaskShift);
CGEventSetFlags(event7, kCGEventFlagMaskCommand);
CGEventPost(kCGSessionEventTap, event7);
CFRelease(event7);
Both are not working. Suggest me any good option.
1] doesn't work because you don't set the event flags. 2] doesn't work because you don't post the key-up event and CGEventSetFlags(event7, kCGEventFlagMaskCommand); replaces the kCGEventFlagMaskShift flag. Set both flags:
CGEventRef event7;
event7 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)85, true);//or 20
CGEventSetFlags(event7, kCGEventFlagMaskShift | kCGEventFlagMaskCommand);
CGEventPost(kCGSessionEventTap, event7);
CFRelease(event7);
event7 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)85, false);//or 20
CGEventSetFlags(event7, kCGEventFlagMaskShift | kCGEventFlagMaskCommand);
CGEventPost(kCGSessionEventTap, event7);
CFRelease(event7);

Map missing chars on italian keyboards

I would like map a chars that doesn't exists on my keyboard layout is it possible?
My keyboard layout is italian and the the chars is ~ and `
I would like remap when I press AltGr (VK_RMENU) + VK_OEM_4 or VK_OEM_6.
But this chars aren't in Virtual-Key Codes list, so I think I coud't use SendInput for remapping...
For others chars SendInput works
But this chars aren't in Virtual-Key Codes list, so I think I coud't
use SendInput for remapping...
The ~ and ` are in the Virtual-Key Codes list. It is VK_OEM_3 (0xC0).
You can use SendInput enter ~ and ` like this:
UINT result = 0;
DWORD errCode = 0;
tagINPUT inputArray[2] = {};
tagKEYBDINPUT keyboardInput[2] = {};
keyboardInput[1].wVk = 0xC0; // Virtual-Key code for the '`~' key.
keyboardInput[1].wScan = 0x29; // Scan code for the '`~' key.
inputArray[1].type = INPUT_KEYBOARD;
inputArray[1].ki = keyboardInput[1];
keyboardInput[0].wVk = 0x10; // Virtual-Key code for the 'shift' key.
keyboardInput[0].wScan = 0x2A; // Scan code for the 'shift' key.
inputArray[0].type = INPUT_KEYBOARD;
inputArray[0].ki = keyboardInput[0];
// Enter '~' (shift + '`~' key)
result = SendInput(2, inputArray, sizeof(tagINPUT));
errCode = GetLastError();
// Enter '`'
result = SendInput(1, &inputArray[1], sizeof(tagINPUT));
errCode = GetLastError();
If you want to enter these two chars the SendMessage also can do this (similar issue):
LPCWSTR Target_window_Name = TEXT("Untitled - Notepad"); //<- Has to match window name
HWND hWindowHandle = FindWindow(NULL, Target_window_Name);
HWND EditClass = FindWindowEx(hWindowHandle, NULL, L"Edit", NULL);
SendMessage(EditClass, WM_KEYDOWN, 0xC0, 0x002C0001); //VK_OEM_3 0xC0
SendMessage(EditClass, WM_CHAR, 0x7E, 0x002C0001); //~
SendMessage(EditClass, WM_KEYUP, 0xC0, 0xC02C0001);
SendMessage(EditClass, WM_KEYDOWN, 0xC0, 0x002C0001); //VK_OEM_3 0xC0
SendMessage(EditClass, WM_CHAR, 0x60, 0x002C0001); //`
SendMessage(EditClass, WM_KEYUP, 0xC0, 0xC02C0001);
References: "SendInput" "SendMessage"
For scan code you can search "Keyboard Scan Code Specification - Microsoft".

Findwindow and SendMessage

I am trying to figure out why this is not sending a ALT+F to notepad,
Private Declare Function FindWindow1 Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const WM_KEYUP = &H101
Private Const WM_KEYDOWN = &H100
Private Const WM_SYSKEYDOWN = &H104
Private Sub Command_Click()
Dim parenthwnd As Long
Dim hwnd As Long
parenthwnd = FindWindow1(vbNullString, "Untitled - Notepad")
retvalue = SendMessage(parenthwnd, WM_SYSKEYDOWN, VK_MENU, 1&)
retvalue = SendMessage(parenthwnd, WM_KEYDOWN, VK_F, 1&)
End Sub
I get a value for parenthwnd but not for any of the retvalue values (0).
What am I missing?
You can try to send/post these messages all you want. Unfortunately they don't effect the return value of GetAsyncKeyState - which is what the system uses, while processing the VK_F message - to see if the alt is down.
As a result you can't fake keystrokes with modifiers to other applications using this mechanism.
To get around this, you need to use SendInput - but this requires that the application to receive the keystrokes is the foreground/focus window.
I think this is the problem: you're sending your message to the frame around the notepad window, and need to send it to the menu window. Use your handle with FindWindowEx and the 32768 class name (that's a menu) to get the menu window, which is a child of the one you've got the handle to. Here are two pages: http://msdn.microsoft.com/en-us/library/ms633500(v=VS.85).aspx and http://msdn.microsoft.com/en-us/library/ms633574(VS.85).aspx#class_name that should help.
Use Postmessage instead of Sendmessage
Public Const WM_SYSKEYDOWN = &H104
PostMessage hwnd, WM_SYSKEYDOWN, vbKeyF, 2 ^ 29
'Simulates Alt + F (2^29 sets the 29 bit of lParam indicating Alt is being pressed.

SendMessage to click on Non-Client items

I am having trouble getting SendMessage to activate a menu on another program. When looking through Spy++ when a user clicks on the menu the following occurs:
000A0628 P WM_NCLBUTTONDOWN nHittest:HTMENU xPos:1706 yPos:395 [wParam:00000005 lParam:018B06AA time:27:23:56.156 point:(1706, 395)]
000A0628 S WM_SYSCOMMAND uCmdType:SC_MOUSEMENU xPos:1706 yPos:395 [wParam:0000F095 lParam:018B06AA]
000A0628 S WM_ENTERMENULOOP fIsTrackPopupMenu:False [wParam:00000000 lParam:00000000]
000A0628 R WM_ENTERMENULOOP lResult:00000000
000A0628 S WM_SETCURSOR hwnd:000A0628 nHittest:HTCAPTION wMouseMsg:0000 [wParam:000A0628 lParam:00000002]
000A0628 R WM_SETCURSOR fHaltProcessing:False [lResult:00000000]
000A0628 S WM_INITMENU hmenuInit:00200355 [wParam:00200355 lParam:00000000]
000C0672 S EM_GETSEL lpdwStart:0010EFA0 lpdwEnd:0010EFA4 [wParam:0010EFA0 lParam:0010EFA4]
000C0672 R EM_GETSEL wStart:26 wEnd:26 lpdwStart:0010EFA0 (684) lpdwEnd:0010EFA4 (276) [lResult:001A001A]
000C0672 S WM_GETTEXTLENGTH wParam:00000000 lParam:00000000
000C0672 R WM_GETTEXTLENGTH cch:26 [lResult:0000001A]
000C0672 S EM_CANUNDO wParam:00000000 lParam:00000000
000C0672 R EM_CANUNDO fCanUndo:True [lResult:00000001]
000A0628 R WM_INITMENU lResult:00000000
000A0628 S WM_MENUSELECT uItem:0 fuFlags:MF_POPUP | MF_HILITE | MF_MOUSESELECT hmenu:00200355 [wParam:80900000 lParam:00200355]
000A0628 R WM_MENUSELECT lResult:00000000
000A0628 S WM_INITMENUPOPUP hmenuPopup:00150595 uPos:0 fSystemMenu:False [wParam:00150595 lParam:00000000]
000A0628 R WM_INITMENUPOPUP lResult:00000000
Whereas when I attempt the same using SendMessage with WM_NCLBUTTONDOWN the following happens instead:
000A0628 P WM_NCLBUTTONDOWN nHittest:HTMENU xPos:1700 yPos:400 [wParam:00000005 lParam:019006A4 time:27:25:02.156 point:(1700, 400)]
000A0628 S WM_SYSCOMMAND uCmdType:SC_MOUSEMENU xPos:1700 yPos:400 [wParam:0000F095 lParam:019006A4]
000A0628 S WM_ENTERMENULOOP fIsTrackPopupMenu:False [wParam:00000000 lParam:00000000]
000A0628 R WM_ENTERMENULOOP lResult:00000000
000A0628 S WM_EXITMENULOOP fIsTrackPopupMenu:False [wParam:00000000 lParam:00000000]
000A0628 R WM_EXITMENULOOP lResult:00000000
000A0628 S WM_MENUSELECT uItem:0 fuFlags:FFFF (menu was closed) hmenu:00000000 [wParam:FFFF0000 lParam:00000000]
000A0628 R WM_MENUSELECT lResult:00000000
000A0628 S WM_EXITMENULOOP fIsTrackPopupMenu:False [wParam:00000000 lParam:00000000]
000A0628 R WM_EXITMENULOOP lResult:00000000
000A0628 R WM_SYSCOMMAND lResult:00000000
Does anyone know how to get this to work properly?
Cheers
Ross
I'd guess that the menu loop tests for the state of the mouse button. However, you haven't simulated pressing the mouse button, all you're doing is sending a message saying that the button is down.
If you need synchronous behavior, I would suggest a windows hook and have your thread block until the right time. (In other words, you can use an asynchronous approach and then wrap it so that it blocks and acts as a synchronous call. It's extra work, but if you insist on synchronous behavior from something that is inherently asynchronous, it's your best solution.)
You shouldn't use SendMessage to simulate mouse clicks and key presses. Use SendInput instead.
You can perfectly use SendMessage(), in particular for menus (see MSDN and Google Groups for code samples)
(and hooks have nothing to do here)

Resources