Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 712
  • Last Modified:

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

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
SniperVishal
Asked:
SniperVishal
  • 7
  • 5
  • 4
  • +1
2 Solutions
 
colr__Commented:
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
 
SniperVishalAuthor Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
colr__Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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
 
SniperVishalAuthor Commented:
the user should not be able to copy the values of the jTable. Its a form using swing.
0
 
SniperVishalAuthor Commented:
the ctrl+c ? how can i do it then? where should the key listener code be placed?
0
 
colr__Commented:
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
 
ksivananthCommented:
I think you need to overide the key mapping/binding for the control to disable the default functionality.
0
 
ksivananthCommented:
for e.g.,
table.getKeymap().addActionForKeyStroke(KeyStroke.getKeyStroke(67, 128), new DefaultEditorKit.CopyAction());
0
 
ksivananthCommented:
I am sorry, it should be inputMap instead of keyMap
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>>  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
 
ksivananthCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
Yes, for overriding default functionality, that might be required.
0
 
ksivananthCommented:
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
 
SniperVishalAuthor Commented:
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
 
ksivananthCommented:
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
 
ksivananthCommented:
>>For the jTable i disabled the cell selection

Just curious, how did u do this?
0
 
SniperVishalAuthor Commented:
jTableVishal.setRowSelectionAllowed(false);
jTableVishal.setCellSelectionEnabled(false);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 7
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now