Link to home
Start Free TrialLog in
Avatar of SniperVishal
SniperVishal

asked on

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?
Avatar of colr__
colr__

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__
Avatar of SniperVishal

ASKER

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...
Avatar of Mayank S
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.
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__
>> 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.
the user should not be able to copy the values of the jTable. Its a form using swing.
the ctrl+c ? how can i do it then? where should the key listener code be placed?
ASKER CERTIFIED SOLUTION
Avatar of colr__
colr__

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think you need to overide the key mapping/binding for the control to disable the default functionality.
for e.g.,
table.getKeymap().addActionForKeyStroke(KeyStroke.getKeyStroke(67, 128), new DefaultEditorKit.CopyAction());
I am sorry, it should be inputMap instead of keyMap
>>  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.
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.
Yes, for overriding default functionality, that might be required.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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.
>>For the jTable i disabled the cell selection

Just curious, how did u do this?
jTableVishal.setRowSelectionAllowed(false);
jTableVishal.setCellSelectionEnabled(false);