Question : how to simplify this

Hello,

Shift: TShiftState;

TShiftState = set of (ssShift, ssAlt, ssCtrl,
    ssLeft, ssRight, ssMiddle, ssDouble);

Ok, but its not compatible with others languages like C++
So i need to make it compatible in my Param, like

TMyParam = packed record
  kbShift: DWORD;
end;

I'm doing in this way...

if ssShift in Shift then
kbShift := Integer(ssShift);
if ssAlt in Shift then
kbShift := kbShift or Integer(ssAlt);
if ssCtrl in Shift then
kbShift := kbShift or Integer(ssCtrl);
//for each type i need to do it, but maybe a loop can do this?
...

So i can call the reverse way like

if ssCtrl in kbShift then
...

But, i think there is a better way to do that... i tried to loop but no success

kbShift := -1;
for i := Low(TShiftState) to High(TShiftState) do
if (TShiftState(i) in Shift) then
kbShift := kbShift or TShiftState(i);
...

not work! hehe

Some idea?

Regards,
Carlos

Answer : how to simplify this

NOTE:
Sounds like you need the virtual key code constant represented in your TShiftstate variable. Using integer(ssShift), integer(ssAlt), etc. won't work as it will return the ordinal value of the set item. i.e. Shift will return 0, alt will return 1, etc. I'm pretty sure this is not what you want.

if you need the virtual key value, use something like this:
var kc: integer;
...
kc:= 0;
if ssShift in AShiftState then kc:= kc OR VK_SHIFT;
if ssCtrl in AShiftState then kc:= kc OR VK_CONTROL;
if ssALT in AShiftState then kc:= kc OR VK_MENU;

if you need to use it for mouse messages, use something like this:
if ssShift in AShiftState then kc:= kc OR MK_SHIFT;
if ssCtrl in AShiftState then kc:= kc OR MK_CONTROL;
//alt not supported in mouse message, you would have to use something like GetKeyState to determine it

Random Solutions  
 
programming4us programming4us