Change the primary mouse button (Views: 100)


How do you change the primary mouse button


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;

  LeftButton = '0';
  RightButton = '1';
  VaueToRead = 'SwapMouseButtons';
  with TRegistry.Create do
      if OpenKey('Control Panel\Mouse', False) then
        if ValueExists(VaueToRead) then
          if ReadString(VaueToRead) = LeftButton then
            WriteString(VaueToRead, RightButton);
            WriteString(VaueToRead, LeftButton);

<< Back to main page