A: In real mode, to access the shift states you would use the following code.
var
ShiftStates: Word;
begin
ShiftStates := MemW[$0040: $0017];
:
:
end;
In protected mode, it is necessary to set up a selector to access memory directly.
var
ShiftSel: Word;
ShiftStates: Word;
begin
ShiftSel := AllocSelector(DSeg);
SetSelectorBase(ShiftSel, $00400);
SetSelectorLimit(ShiftSel, $10000);
ShiftStates := MemW[ShiftSel: $0017];
:
:
end;
Notice that in SetSelectorBase the value $00400 is used instead of $0040.
The value represents a complete 20-bit linear address. For instance,
if you needed to set a selector to point to the real mode segment $D000,
you would use the value $D0000 in the call to SetSelectorBase.