473,322 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

how to create a shortcut?

mishela
11
I wonder how do I create a shortcut in java, for example Ctrl+S for save and so on.. I tried to make a keyEvent for the whole frame, but it didn't work. Can somebody help me with this please?
May 9 '07 #1
7 3175
JosAH
11,448 Expert 8TB
I wonder how do I create a shortcut in java, for example Ctrl+S for save and so on.. I tried to make a keyEvent for the whole frame, but it didn't work. Can somebody help me with this please?
Did you read the API documentation for the JMenuItem class?

kind regards,

Jos
May 9 '07 #2
mishela
11
i know how to create a menu and save a program from it. But I want the whole window to react to the keys alt and then S. I know it is made throu focus, but it does not work.. I have this:

private void formKeyReleased(java.awt.event.KeyEvent evt) {
switch(evt.getKeyCode()) {
case KeyEvent.CTRL_DOWN_MASK: isCtrlPressed = false; break;
case KeyEvent.ALT_DOWN_MASK: isAltPressed = false; break;
case KeyEvent.SHIFT_DOWN_MASK: isShiftPressed = false; break;
}
this.requestFocus();
}

private void formKeyPressed(java.awt.event.KeyEvent evt) {
switch(evt.getKeyCode()) {
case KeyEvent.CTRL_DOWN_MASK: isCtrlPressed = true; break;
case KeyEvent.ALT_DOWN_MASK: isAltPressed = true; break;
case KeyEvent.SHIFT_DOWN_MASK: isShiftPressed = true; break;
case KeyEvent.VK_A: {
if(isAltPressed) {
addAction();
}
}
case KeyEvent.VK_E: {
if(isAltPressed) {
editAction();
}
}
case KeyEvent.VK_D: {
if(isAltPressed) {
deleteAction();
}
}
case KeyEvent.VK_N: {
if(isCtrlPressed) {
newAction();
}
}
case KeyEvent.VK_S: {
if(isCtrlPressed) {
if(isAltPressed) {
saveAsAction();
} else saveAction();
}
}
case KeyEvent.VK_O: {
openAction();
}
case KeyEvent.VK_F4: {
exitAction();
}
case KeyEvent.VK_TAB: {
if(isCtrlPressed) {
if(tabbedPane.getSelectedComponent() == employeePanel) {
tabbedPane.setSelectedComponent(animalPanel);
}
}
}
}

this.requestFocus();
}

i don't know what's wrong..
May 9 '07 #3
JosAH
11,448 Expert 8TB
If you instantiate a JMenuItem given a KeyStroke, the 'entire window' will listen
to that accellerator key; the Alt-S key:
Expand|Select|Wrap|Line Numbers
  1. KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.ALT_MASK)
If you pass the above keystroke to the ctor, everything will work as expected.

kind regards,

Jos
May 9 '07 #4
mishela
11
wow, thanks :)
May 9 '07 #5
mishela
11
there is one more problem, how do I make Alt+Ctrl+S?
May 9 '07 #6
mishela
11
I already have it and it works, thank you. I would like to make a keystroke for button, too. Can you please write how do I do this?
May 9 '07 #7
JosAH
11,448 Expert 8TB
wow, thanks :)
I was mistaken actually: you can't pass KeyStrokes to a ctor of a JMenuItem:
you have to set it using the setAccellerator() method, but I guess you already
figured that out.

Buttons don't have accellerator keys, they just have mnemonics; read the API docs.
But if you have a menu item with a such a key, you can of course define the
same mnemonic for a button.

kind regards,

Jos
May 9 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Eskimo Joe | last post by:
I am trying to create a desktop icon using VB6. is this possible? -p
1
by: Tom | last post by:
I created an msi using the Setup Wizard, but don't seem to be able to create a shortcut to the applicaton on the desktop (or prompt for one) or the menu. Also, I would like to add an "uninstall"...
4
by: I_AM_DON_AND_YOU? | last post by:
There is one more problem I am facing but didn't get the solution. In my Setup Program I am not been able to create 2 things (when the program is intalled on the client machine ) : (1) create...
3
by: Michael D. Murphy | last post by:
Hi, I am searching for a way to creat desktop shortcuts when running the setup files that were created within a VB solution using the setup wizard. Thanks, Michael
7
by: GrandpaB | last post by:
I would appreciate assistance learning how to create a Desktop shortcut in my setup project. In the left pane of the Setup/File System window I right-clicked User's Desktop. From the contex...
3
by: baroque Chou | last post by:
We build a web site and have some registred user, we want to offer such a feature that the user can access our site from his or her desktop shortcut and want it be log on automatic. So I plan to...
3
by: thi | last post by:
Hi, How do i create an application shortcut on desktop programmically in C#? I have done some shortcut coding before in visual basic 6 using windows scripting host but later found out that...
3
by: sanghavi | last post by:
how to create a set up project in vb.net..how to run an application on a different machine
3
by: jojo41300000 | last post by:
Hi, I am trying to create a shortcut icon on Today in window mobile 5.0. I have a CAB file which will install the program and create the shortcut icon in Today. The CAB file sucessfully...
1
by: bipi | last post by:
Hi everybody, I deploy my project by using .NET deployment. I create shortcut at Desktop, but this shortcut does not have "Run as..." option. How can I create shortcut with "Run as..." option...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.