Solved

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

Posted on 2006-06-12
22
662 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

778 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