Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-06-12
22
Medium Priority
?
689 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 500 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 500 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

609 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