简体   繁体   English

Java:如何在 OS X Lion 中获取滚动方式?

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

Since OS X supports the "natural scrolling", my applications works wrong.由于 OS X 支持“自然滚动”,所以我的应用程序运行错误。 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.所以,我想做的是检查 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.如果它是“自然的”,我会从MouseWheelEvent.getWheelRotation()中获取与滚动值相反的值,以使我的放大/缩小行为感觉正确。

So, in short: How to know if OS X uses natural scrolling or not?所以,简而言之:如何知道 OS X 是否使用自然滚动?

Found a solution.找到了解决方案。

First, you need a library to read.plist files.首先,您需要一个库来读取 .plist 文件。 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:您可以轻松地在 GlobalPreferneces.plist 中阅读(使用 fseventer 检查更改滚动选项时更改了哪个文件)以找出启用了哪种滚动,如下所示:

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());
}

As Apple has dropped Java, I don't think that there is built in method to detect if natural scrolling is enabled.由于 Apple 已经放弃了 Java,我认为没有内置方法来检测是否启用了自然滚动。 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.但是,您可以读入用于配置鼠标/触摸板行为的 .plist 文件(这是一个基本的 xml 文件)并查找启用自然滚动的属性设置为 true 或 false。

You can find the required.plist files here:您可以在此处找到所需的.plist 文件:

User/Library/Preferences/ <- This folder is hidden in Lion! User/Library/Preferences/ <- 这个文件夹隐藏在 Lion 中!

com.apple.driver.AppleBluetoothMultitouch.mouse.plist com.apple.driver.AppleBluetoothMultitouch.mouse.plist

com.apple.driver.AppleHIDMouse.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.您无法使用标准 Java 框架读取 plist 文件,因为 Mac OS 10.4 all.plists 以二进制格式保存。 See my other answer for a correct solution.请参阅我的其他答案以获得正确的解决方案。

Take a look at Mike Swingler's answer on the java-dev mailing list.在 java-dev 邮件列表中查看 Mike Swingler 的回答 There is a whole thread about it.关于它有一个完整的线索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM