Martijn Courteaux
Martijn Courteaux

Reputation: 68847

Java: How to get the scrolling method in OS X Lion?

Since OS X supports the "natural scrolling", my applications works wrong. The natural scrolling is made for scroll panes, which I really like. But, when I want to zoom in/out, it works wrong. So, what I want to do is check the scroll method for OS X.
If it is "natural" I'll take the opposite of the scroll values from MouseWheelEvent.getWheelRotation() to make my zoom in/out behavior feel correct.

So, in short: How to know if OS X uses natural scrolling or not?

Upvotes: 6

Views: 1275

Answers (3)

yan.kun
yan.kun

Reputation: 6908

As Apple has dropped Java, I don't think that there is built in method to detect if natural scrolling is enabled. However, you could read in in the .plist files for configuring mouse/touchpad behaviour (which is a basic xml file) and look for the property to enable natural scrolling is set to true or false.

You can find the required .plist files here:

User/Library/Preferences/ <- This folder is hidden in Lion!

com.apple.driver.AppleBluetoothMultitouch.mouse.plist

com.apple.driver.AppleHIDMouse.plist

Edit:

You can't read in a plist file with the standard Java Framework, as since Mac OS 10.4 all .plists are saved in binary format. See my other answer for a correct solution.

Upvotes: 1

yan.kun
yan.kun

Reputation: 6908

Found a solution.

First, you need a library to read .plist files. I used this one.

Than you can easily read in the GlobalPreferneces.plist (checked with fseventer which file is changed when changing the scroll option) to find out which kind of scrolling is enabled like this:

try {
    File globalPref = new File(System.getProperty("user.home") + "/Library/Preferences/.GlobalPreferences.plist");
    NSDictionary dict = (NSDictionary)PropertyListParser.parse(globalPref);

    NSNumber pref = (NSNumber)dict.objectForKey("com.apple.swipescrolldirection");

    if(pref.boolValue()) {
        //natural scrolling is enabled
    }  
} catch (Exception ex) {
    System.out.println("Faild to parse plist: " + ex.getMessage());
}

Upvotes: 3

Damien
Damien

Reputation: 3220

Take a look at Mike Swingler's answer on the java-dev mailing list. There is a whole thread about it.

Upvotes: 1

Related Questions