2012-10-03 29 views
8

मैं जावाएफएक्स निमोनिक काम करने की कोशिश कर रहा हूं। मेरे पास दृश्य पर कुछ बटन है और मैं जो हासिल करना चाहता हूं वह है Ctrl + S दबाकर इस बटन ईवेंट को आग लगाना।जावाएफएक्स 2.2 माइमोनिक (और त्वरक) का उपयोग

@FXML 
public Button btnFirst; 

btnFirst.getScene().addMnemonic(new Mnemonic(btnFirst, 
      new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN))); 

बटन mnemonicParsing गलत है: यहाँ एक कोड sceleton है। (ठीक है, इस काम को करने की कोशिश करते समय मैंने इसे सही करने की कोशिश की है, लेकिन कोई परिणाम नहीं)। जावाएफएक्स प्रलेखन में कहा गया है कि जब एक दृश्य पर एक निमोनिक पंजीकृत होता है, और कीकंबिनेशन दृश्य को बिना सेंसर तक पहुंचाता है, तो लक्ष्य नोड को एक्शनवेन्ट भेजा जाएगा। लेकिन यह काम नहीं करता है, शायद, मैं गलत कर रहा हूं ...

मैं मानक बटन के निमोनिक का उपयोग कर सकता हूं (अंडरस्कोर वर्ण द्वारा वास्तविक और उपसर्ग 'एफ' अक्षर में mnemonicParsing को सेट करके)। लेकिन इस तरह उपयोगकर्ता को Alt कुंजी का उपयोग करना पड़ता है, जो मेनू बार के साथ ब्राउज़र पर कुछ अजीब व्यवहार लाता है (अगर एप्लिकेशन को Alt + S दबाकर फायरिंग बटन ईवेंट के बाद सक्रिय ब्राउज़र के मेनू से वेब पेज में एम्बेड किया गया है)। इसके अलावा, मानक तरीका Ctrl + Shift + F3 जैसे शॉर्टकट बनाना असंभव बनाता है।

तो, अगर इस काम को करने का कोई तरीका है?

उत्तर

20

आपके उपयोग के मामले में, मुझे लगता है कि आप वास्तव में एक नींबू के बजाय त्वरक का उपयोग करना चाहते हैं।

button.getScene().getAccelerators().put(
    new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN), 
    new Runnable() { 
    @Override public void run() { 
     button.fire(); 
    } 
    } 
); 

ज्यादातर मामलों में यह अनुशंसित है कि आप, संशोधक विनिर्देशक के रूप में KeyCombination.SHORTCUT_DOWN का उपयोग उपरोक्त कोड में के रूप में। इसका एक अच्छा स्पष्टीकरण KeyCombination दस्तावेज में है:

शॉर्टकट संशोधक संशोधक कुंजी जो मेजबान मंच पर कीबोर्ड शॉर्टकट में आमतौर पर इस्तेमाल किया है प्रतिनिधित्व करने के लिए प्रयोग किया जाता है। यह मैक पर विंडोज और मेटा (कमांड कुंजी) पर उदाहरण नियंत्रण के लिए है। का उपयोग करके शॉर्टकट कुंजी संशोधक डेवलपर प्लेटफॉर्म स्वतंत्र शॉर्टकट बना सकते हैं। इसलिए "शॉर्टकट + सी" कुंजी संयोजन को आंतरिक रूप से को विंडोज़ पर "Ctrl + C" और मैक पर "मेटा + सी" के रूप में संभाला जाता है।

आप विशेष रूप से कोड केवल एक Ctrl + S कुंजी संयोजन को संभालने के लिए करना चाहता था, वे आप इस्तेमाल कर सकते हैं:

import javafx.animation.*; 
import javafx.application.Application; 
import javafx.event.*; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.scene.image.*; 
import javafx.scene.input.*; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class SaveMe extends Application { 
    @Override public void start(final Stage stage) throws Exception { 
    final Label response = new Label(); 
    final ImageView imageView = new ImageView(
     new Image("http://icons.iconarchive.com/icons/gianni-polito/colobrush/128/software-emule-icon.png") 
    ); 
    final Button button = new Button("Save Me", imageView); 
    button.setStyle("-fx-base: burlywood;"); 
    button.setContentDisplay(ContentDisplay.TOP); 
    displayFlashMessageOnAction(button, response, "You have been saved!"); 

    layoutScene(button, response, stage); 
    stage.show(); 

    setSaveAccelerator(button); 
    } 

    // sets the save accelerator for a button to the Ctrl+S key combination. 
    private void setSaveAccelerator(final Button button) { 
    Scene scene = button.getScene(); 
    if (scene == null) { 
     throw new IllegalArgumentException("setSaveAccelerator must be called when a button is attached to a scene"); 
    } 

    scene.getAccelerators().put(
     new KeyCodeCombination(KeyCode.S, KeyCombination.SHORTCUT_DOWN), 
     new Runnable() { 
     @Override public void run() { 
      fireButton(button); 
     } 
     } 
    ); 
    } 

    // fires a button from code, providing visual feedback that the button is firing. 
    private void fireButton(final Button button) { 
    button.arm(); 
    PauseTransition pt = new PauseTransition(Duration.millis(300)); 
    pt.setOnFinished(new EventHandler<ActionEvent>() { 
     @Override public void handle(ActionEvent event) { 
     button.fire(); 
     button.disarm(); 
     } 
    }); 
    pt.play(); 
    } 

    // displays a temporary message in a label when a button is pressed, 
    // and gradually fades the label away after the message has been displayed. 
    private void displayFlashMessageOnAction(final Button button, final Label label, final String message) { 
    final FadeTransition ft = new FadeTransition(Duration.seconds(3), label); 
    ft.setInterpolator(Interpolator.EASE_BOTH); 
    ft.setFromValue(1); 
    ft.setToValue(0); 
    button.setOnAction(new EventHandler<ActionEvent>() { 
     @Override public void handle(ActionEvent event) { 
     label.setText(message); 
     label.setStyle("-fx-text-fill: forestgreen;"); 
     ft.playFromStart(); 
     } 
    }); 
    } 

    private void layoutScene(final Button button, final Label response, final Stage stage) { 
    final VBox layout = new VBox(10); 
    layout.setPrefWidth(300); 
    layout.setAlignment(Pos.CENTER); 
    layout.getChildren().addAll(button, response); 
    layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 20; -fx-font-size: 20;"); 
    stage.setScene(new Scene(layout)); 
    } 

    public static void main(String[] args) { launch(args); } 
} 
// icon license: (creative commons with attribution) http://creativecommons.org/licenses/by-nc-nd/3.0/ 
// icon artist attribution page: (eponas-deeway) http://eponas-deeway.deviantart.com/gallery/#/d1s7uih 

नमूना उत्पादन:

new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN) 

यहाँ एक निष्पादन योग्य उदाहरण है:

Sample program output

+0

गहने, धन्यवाद यो यू। आप बिल्कुल सही, त्वरक मेरे उपयोग के मामले हैं। मैंने गलती से सोचा कि त्वरक को मेनू आइटम से कनेक्ट होना होगा। आपके पाठ के लिए धन्यवाद। – bes67

+2

नोट: अपने एप्लिकेशन प्लेटफार्म-स्वतंत्र रखने के लिए "नियंत्रण" (विंडोज) या "मेटा" (मैक) पर "शॉर्टकट" पसंद करें। – Puce

+2

धन्यवाद पुस, 'CONTROL_DOWN' की बजाय' SHORTCUT_DOWN' का उपयोग करना अच्छा है। मैंने इस सिफारिश को शामिल करने के लिए उत्तर अपडेट किया। – jewelsea