Solved

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

Posted on 2006-06-12
22
667 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Mayank S
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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:Mayank S
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
 
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:Mayank S
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:Mayank S
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Opening PDF on button click and fill new document 2 54
spring jars download 1 53
import as existing maven project 3 41
Java List 4 41
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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

726 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