Criando Teclas de Atalho

E ae pessoal, queria uma ajuda pra criar uma tecla de atalho para um botão.

Dei uma pesquisada e usei isso aqui:

btNovo.setMnemonic(KeyEvent.VK_N);

E isso gera o atalho Alt + N para chamar o botão, porém estou procurando pra fazer Ctrl + N.

Tem como fazer isso?

Valeu :smiley:

este aqui, funciona com o enter…

private void jTletraKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:

    if (evt.getKeyCode() == 10) {
        JOptionPane.showMessageDialog(null, "ENTER!", "Letra ja dita", JOptionPane.INFORMATION_MESSAGE);

    }
}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {


}[code]

o 10 é o codigo ascii, do enter…
da para usar varias, coisas, ja para usar junto com o ctrl, nao sei… agora… mas uma tecla so, ja da de fazer… com alt tambem conheço.

segue uma imagem da tabela ascii, no atachement


pesquisei,

ja teve isto no guj… olha la

[code]
/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */

package testesgeraisetc;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class KeyTester {
static class MyActionListener extends AbstractAction {
MyActionListener(String s) {
super(s);
}

public void actionPerformed(ActionEvent e) {
  System.out.println(getValue(Action.NAME));
}

}

public static void main(String args[]) {
String actionKey = “theAction”;
JFrame f = new JFrame(“Key Tester”);
JButton jb1 = new JButton(“B
Focused/Typed”);
JButton jb2 = new JButton(“Ctrl-C
Window/Pressed”);
JButton jb3 = new JButton(“Shift-D
Ancestor/Released”);
Container pane = f.getContentPane();
pane.add(jb1, BorderLayout.NORTH);
pane.add(jb2, BorderLayout.CENTER);
pane.add(jb3, BorderLayout.SOUTH);

KeyStroke stroke = KeyStroke.getKeyStroke("typed A");
Action action = new MyActionListener("Action Happened");
// Defaults to JComponent.WHEN_FOCUSED map
InputMap inputMap = jb1.getInputMap();
inputMap.put(stroke, actionKey);
ActionMap actionMap = jb1.getActionMap();
actionMap.put(actionKey, action);

stroke = KeyStroke.getKeyStroke("ctrl F");
action = new MyActionListener("Action Didn't Happen");
inputMap = jb2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(stroke, actionKey);
actionMap = jb2.getActionMap();
actionMap.put(actionKey, action);

stroke = KeyStroke.getKeyStroke("shift released D");
action = new MyActionListener("What Happened?");
inputMap = jb3
    .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(stroke, actionKey);
actionMap = jb3.getActionMap();
actionMap.put(actionKey, action);

f.setSize(200, 200);
f.show();

}
}[/code]

O PROGRAMA É BEM INTERESSANTE. AQUI MUDEI PARA FUNCIONAR… COM CONTROL F E COM A LETRA A… É SÓ MUDAR ALI QUE MUDA MESMO. JA O SHIFT D, tem que clicar no botao do shift d, para funcionar, nao entendi muito bem o porque, mas aí responde.

aqui tudo com control, as vezes funciona um ou outro, depende o botao que clica…
mas se quiser uma função só, acho que não vai haver problemas, vale o teste. valeu…

[code]
/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */

package testesgeraisetc;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class KeyTester {
static class MyActionListener extends AbstractAction {
MyActionListener(String s) {
super(s);
}

public void actionPerformed(ActionEvent e) {
  System.out.println(getValue(Action.NAME));
}

}

public static void main(String args[]) {
String actionKey = “theAction”;
JFrame f = new JFrame(“Key Tester”);
JButton jb1 = new JButton(“B
Focused/Typed”);
JButton jb2 = new JButton(“Ctrl-C
Window/Pressed”);
JButton jb3 = new JButton(“Shift-D
Ancestor/Released”);
Container pane = f.getContentPane();
pane.add(jb1, BorderLayout.NORTH);
pane.add(jb2, BorderLayout.CENTER);
pane.add(jb3, BorderLayout.SOUTH);

KeyStroke stroke = KeyStroke.getKeyStroke("ctrl A");
Action action = new MyActionListener("Action Happened");
// Defaults to JComponent.WHEN_FOCUSED map
InputMap inputMap = jb1.getInputMap();
inputMap.put(stroke, actionKey);
ActionMap actionMap = jb1.getActionMap();
actionMap.put(actionKey, action);

stroke = KeyStroke.getKeyStroke("ctrl F");
action = new MyActionListener("Action Didn't Happen");
inputMap = jb2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(stroke, actionKey);
actionMap = jb2.getActionMap();
actionMap.put(actionKey, action);

stroke = KeyStroke.getKeyStroke("ctrl D");
action = new MyActionListener("What Happened?");
inputMap = jb3
    .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(stroke, actionKey);
actionMap = jb3.getActionMap();
actionMap.put(actionKey, action);

f.setSize(200, 200);
f.show();

}
}[/code]

funciona tudo normal, se focar tudo para a janela…

InputMap inputMap = jb1.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

:shock:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testesgeraisetc;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class KeyTester {
  static class MyActionListener extends AbstractAction {
    MyActionListener(String s) {
      super(s);
    }

    public void actionPerformed(ActionEvent e) {
      System.out.println(getValue(Action.NAME));
    }
  }

  public static void main(String args[]) {
    String actionKey = "theAction";
    JFrame f = new JFrame("Key Tester");
    JButton jb1 = new JButton("<html><center>B<br>Focused/Typed");
    JButton jb2 = new JButton("<html><center>Ctrl-C<br>Window/Pressed");
    JButton jb3 = new JButton("<html><center>Shift-D<br>Ancestor/Released");
    Container pane = f.getContentPane();
    pane.add(jb1, BorderLayout.NORTH);
    pane.add(jb2, BorderLayout.CENTER);
    pane.add(jb3, BorderLayout.SOUTH);

    KeyStroke stroke = KeyStroke.getKeyStroke("typed B");
    Action action = new MyActionListener("Action Happened");
    // Defaults to JComponent.WHEN_FOCUSED map
    InputMap inputMap = jb1.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, actionKey);
    ActionMap actionMap = jb1.getActionMap();
    actionMap.put(actionKey, action);

    stroke = KeyStroke.getKeyStroke("ctrl C");
    action = new MyActionListener("Action Didn't Happen");
    inputMap = jb2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, actionKey);
    actionMap = jb2.getActionMap();
    actionMap.put(actionKey, action);

    stroke = KeyStroke.getKeyStroke("shift released D");
    action = new MyActionListener("What Happened?");
    inputMap = jb3
        .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, actionKey);
    actionMap = jb3.getActionMap();
    actionMap.put(actionKey, action);

    f.setSize(200, 200);
    f.show();
  }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testesgeraisetc;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class KeyTester {
  static class MyActionListener extends AbstractAction {
    MyActionListener(String s) {
      super(s);
    }

    public void actionPerformed(ActionEvent e) {
      System.out.println(getValue(Action.NAME));
    }
  }

  public static void main(String args[]) {
    String actionKey = "theAction";
    JFrame f = new JFrame("Key Tester");
    JButton jb1 = new JButton("<html><center>B<br>Focused/Typed");
    JButton jb2 = new JButton("<html><center>Ctrl-C<br>Window/Pressed");
    JButton jb3 = new JButton("<html><center>Shift-D<br>Ancestor/Released");
    Container pane = f.getContentPane();
    pane.add(jb1, BorderLayout.NORTH);
    pane.add(jb2, BorderLayout.CENTER);
    pane.add(jb3, BorderLayout.SOUTH);

    KeyStroke stroke = KeyStroke.getKeyStroke("typed B");
    Action action = new MyActionListener("Action Happened");
    // Defaults to JComponent.WHEN_FOCUSED map
    InputMap inputMap = jb1.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, actionKey);
    ActionMap actionMap = jb1.getActionMap();
    actionMap.put(actionKey, action);

    stroke = KeyStroke.getKeyStroke("ctrl C");
    action = new MyActionListener("Action Didn't Happen");
    inputMap = jb2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, actionKey);
    actionMap = jb2.getActionMap();
    actionMap.put(actionKey, action);

    stroke = KeyStroke.getKeyStroke("shift released D");
    action = new MyActionListener("What Happened?");
    inputMap = jb3
        .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, actionKey);
    actionMap = jb3.getActionMap();
    actionMap.put(actionKey, action);

    f.setSize(200, 200);
    f.show();
  }
}