Mirror

Change the primary mouse button (Views: 100)


Problem/Question/Abstract:

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.

uses Windows, Registry;

const
  LeftButton = '0';
  RightButton = '1';
  VaueToRead = 'SwapMouseButtons';
begin
  with TRegistry.Create do
  begin
    try
      if OpenKey('Control Panel\Mouse', False) then
      begin
        if ValueExists(VaueToRead) then
          if ReadString(VaueToRead) = LeftButton then
          begin
            SwapMouseButton(True);
            WriteString(VaueToRead, RightButton);
          end
          else
          begin
            SwapMouseButton(False);
            WriteString(VaueToRead, LeftButton);
          end;
        CloseKey;
      end;
    finally
      Free;
    end;
  end;
end.

<< Back to main page