Author: Kevin Gallagher
How do you change the primary mouse button
Answer:
To change the primary mouse button in code you need to execute an API function
called SwapMouseButton. This changes the primary button but does not alert the
control panel applet for the mouse that the primary button has changed. To do this
we need to write to the registry. The code below shows how to toggle the primary
mouse button by first reading the registry to determine the current assignment then
does the toggle by writing to the registry and executing the SwapMouseButton
function.
1 uses Windows, Registry;
2 3 const4 LeftButton = '0';
5 RightButton = '1';
6 VaueToRead = 'SwapMouseButtons';
7 begin8 with TRegistry.Create do9 begin10 try11 if OpenKey('Control Panel\Mouse', False) then12 begin13 if ValueExists(VaueToRead) then14 if ReadString(VaueToRead) = LeftButton then15 begin16 SwapMouseButton(True);
17 WriteString(VaueToRead, RightButton);
18 end19 else20 begin21 SwapMouseButton(False);
22 WriteString(VaueToRead, LeftButton);
23 end;
24 CloseKey;
25 end;
26 finally27 Free;
28 end;
29 end;
30 end.