WM_KEYDOWN
-
Hi, wie bekomme ich aus einem WM_KEYDOWN-Event den Scancode und virtual Key herraus? Zudem wüsste ich gerne, ob der Key "extended" ist und "repeated" wurde.
-
-
Enumerator schrieb:
Hi, wie bekomme ich aus einem WM_KEYDOWN-Event den Scancode und virtual Key herraus? Zudem wüsste ich gerne, ob der Key "extended" ist und "repeated" wurde.
Scherzfrage? Hast du dir mal angesehen, wie WPARAM und LPARAM bei dieser Nachricht belegt sind?
wParam
Specifies the virtual-key code of the nonsystem key.
lParam
Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table.
0-15
Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.
16-23
Specifies the scan code. The value depends on the OEM.
24
Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
25-28
Reserved; do not use.
29
Specifies the context code. The value is always 0 for a WM_KEYDOWN message.
30
Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is zero if the key is up.
31
Specifies the transition state. The value is always zero for a WM_KEYDOWN message.
-
Das habe ich auch gesehen, aber wie bekomme ich jetzt die entsprechenden Werte aus dem lParam? Muss ja ganz trivial sein, wenn man sich nicht mal genötig sah ein Beispiel auf der msdn anzugeben.
-
Mit & und >>
-
Mensch jetzt lasst euch doch nicht alles aus der Nase ziehen. Gehts noch ein wenig genauer?
Ergibt folgendes also meinen extended Wert?
bool isExtended = lParam << 24;
-
Nö.
bool isExtended = (lParam & 0x1000)!=0;
PS: Das sind Basics... muss man Dir alles vorkauen?
-
Martin Richter schrieb:
bool isExtended = (lParam & 0x1000)!=0;
PS: Das sind Basics... muss man Dir alles vorkauen?
Eigentlich ist 0x1000 etwas wenig für Bit 24 ?
-
Stimmt fehlen 3 Nullen. Ich habe nur an 3 Bytes im Shift gedacht und danmn sind 3 Nullen draus geworden. Sorry!
-
Martin Richter schrieb:
Stimmt fehlen 3 Nullen.
:edit stimmt so.
zum selbst nachzählen:
1000 0000 0000 0000 0000 0000 = 0x800000
1 0000 0000 0000 0000 0000 0000 = 0x1000000
Weil das Zählen von Nullen irgendwie lästig ist, sollte man das
evtl. doch mit shiften (nach rechts!), oder noch besser mit Bitfeld lösen.struct Bits { WORD nRepeatCount: 16; BYTE nScanCode : 8; BYTE nExtended : 1; BYTE nReserved : 4; BYTE nContext : 1; BYTE nPrevious : 1; BYTE nTransition : 1; }; union KeyInfo { LPARAM lParam; Bits bits; };
-
merano schrieb:
zum selbst nachzählen:
1000 0000 0000 0000 0000 0000 = 0x800000
Wie war das mit den Grundlagen
Fängt man nicht bei 0 an zu zählen?
-
Belli schrieb:
Fängt man nicht bei 0 an zu zählen?
Ja, stimmt
Wenn man sich den Vorschlag mit dem Bitfeld ansieht braucht man
nicht zählen.merano schrieb:
struct Bits { WORD nRepeatCount: 16; BYTE nScanCode : 8; BYTE nExtended : 1;
Für das Bit nExtended berechnet ergibt das 16+8+1 = 25
-
Das mit dem Bitfield ist keine gute Idee. Erstens weil das Layout eines Bitfield vollkommen implementation defined ist und zweitens weil der union Hack Undefined Behavior ist.
Ich würd sowas in der Art verwenden:
template <unsigned int n, unsigned int N = 1U> unsigned int extractBits(unsigned int value) { static_assert(n + 1U > N, "does not compute"); return (value >> (n + 1U - N)) & ((1U << N) - 1U); } unsigned int bla = extractBits<4, 3>(42U);