Solved

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

Posted on 2006-06-12
22
665 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: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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
Oracle SQL syntax check  without executing 6 74
varialbe initialization 11 37
MySqlDump not dumping triggers 1 43
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

809 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