Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 370
Martijn Courteaux Avatar asked Aug 16 '11 07:08

Martijn Courteaux


1 Answers

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());
}
like image 171
yan.kun Avatar answered Sep 16 '22 22:09

yan.kun