Solved

How to disable keyboard keys (printscrn, ctrl+c)?

Posted on 2006-06-12
22
654 Views
Last Modified: 2012-08-14
I would like to know how can i disable keyboard keys -- printscrn, ctrl+c in a java application? Is there any class or way to do it?
0
Comment
Question by:SniperVishal
  • 7
  • 5
  • 4
  • +1
22 Comments
 
LVL 8

Expert Comment

by:colr__
ID: 16886199
What do you mean by 'disable'? What type of aplication is it?

You could add a key listener which takes no action when those keys are pressed? But that would only work within your own application. If your looking to disbale these keys system wide, then Java isnt your tool.

colr__
0
 

Author Comment

by:SniperVishal
ID: 16886221
I got a form (GUI) with a jTable in it and want to disable the ctrl+c and print scrn keys so that no one can copy the contents of the jTable and screen also cannot be captured...
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16886232
If its a console application, I don't think you can do anything about it. Ctrl-C on the JVM window will probably terminate it - the best you can do is write a shut-down hook and catch it to do some final clean-up.
0
 
LVL 8

Expert Comment

by:colr__
ID: 16886240
You can do this ONLY when yor GUI is ion focus. If a user was to bring another window into focus, but your GUI was still visible, then you wont be able to disable this functionality.

colr__
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16886241
>> print scrn keys

Not sure if you can do that because that key is likely to be caught by the OS directly, not the Java app.
0
 

Author Comment

by:SniperVishal
ID: 16886248
the user should not be able to copy the values of the jTable. Its a form using swing.
0
 

Author Comment

by:SniperVishal
ID: 16886266
the ctrl+c ? how can i do it then? where should the key listener code be placed?
0
 
LVL 8

Accepted Solution

by:
colr__ earned 125 total points
ID: 16886446
This link shows you how to set it up: http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html.

If it were me, Id get your main GUI class to implement the KeyListener interface, although if its ab igger application you might want to put it in a seperat class. The action handlers would then go in the same class.

Note that as mayankeagle  says though, the CTRL+C is an OS function and so might not be catchable by the java app (in order to disable it).

colr__
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16890043
I think you need to overide the key mapping/binding for the control to disable the default functionality.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 26

Expert Comment

by:ksivananth
ID: 16890081
for e.g.,
table.getKeymap().addActionForKeyStroke(KeyStroke.getKeyStroke(67, 128), new DefaultEditorKit.CopyAction());
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16890302
I am sorry, it should be inputMap instead of keyMap
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16891833
>>  where should the key listener code be placed

Perhaps you should add it to the JTable and the panel/ frame which contains it. The same listener can be used, which ignores the event.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16891887
I don't think, the key lestener will work because if the key is already mapped to some action, the listener is not going to consume the action. So it is better to override the mapping.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16891902
Yes, for overriding default functionality, that might be required.
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 125 total points
ID: 16891942
the below code will work for u,

Action doNothing = new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        //do nothing
    }
};
component.getInputMap().put(KeyStroke.getKeyStroke("F2"),
                            "doNothing");
component.getActionMap().put("doNothing",
                             doNothing);

replace the F2 key with ctrl + c appropriately as below,

KeyStroke.getKeyStroke( new Character( 'c' ), InputEvent.CTRL_MASK ) ;
0
 

Author Comment

by:SniperVishal
ID: 16901404
For the jTable i disabled the cell selection and the user cannot copy the values of the cells.

For the print scrn is there any way?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16904709
I don't think, you can consume that but you can try it in the key press event. On key press, you ma greyout the frame's glasspane or minimze and then maximize. This may work if the system event gets fired on key release.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16904716
>>For the jTable i disabled the cell selection

Just curious, how did u do this?
0
 

Author Comment

by:SniperVishal
ID: 16914232
jTableVishal.setRowSelectionAllowed(false);
jTableVishal.setCellSelectionEnabled(false);
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now