Is there a way to cancel a keystroke?

When a user Scans a barcode representing a # sign while focus is on the fromComboBox, code is used to scroll through the list. It scrolls through the list fine, but then it moves to the next field on the screen, because the scanner (that is what we use for input) is setup to put a return character at the end of each scan (required). Is there a way to stop/cancel the return character from being read/processed? Below is a small sample program that demos the problem. You can run it and select one of the items in the combo. Then when I do a scan of the barcode to scroll, it scrolls, then moves to the next field (It should stay on the the combobox).

So the value returned by getItemCount is the data from the scanner? It's hard to see in all this, but maybe that's the case. If so, it's an int, there's no carriage return there. That suggests to me that the value from the scanner is processed elsewhere, most likely in your fromComboBox.
Or perhaps it's one of the values that you're getting back in the last bit, in the try block. That looks more likely, come to look at it. I don't know what sort of value you're expecting there, but if you're complaining about carriage returns, maybe it's a string. If so, check whether thatValue.equals("\n"). You'll have to assign the value from getSelectedItem into a holding variable in order to do that, remember - you don't want to call getSelectedItem twice and drop one of the values.

jon.kiparsky - getItemCount is just the number of items in the fromComboBox. With the scan gun, I scan a barcode that is a "#" sign. The scanner reads this and interprets it as a # sign with a carriage return at the end. So when I step through my code it will go through the fromComboBoxKeyKeyPressed event 3 times. First time KeyCode = 16 (Data Link), second time is Keycode 51 (# sign w/ shift down), third time is KeyCode 10 (carriage return). So on the second pass the fromComboBox moves to the next item (this is working fine), but then on the third pass it shows that bScroll = false so it moves to the next field. This is very confusing as it seems to behave differently depending on if I step through the code in debug mode or if I just run it full speed.

NormR1 -- I added the e.consume in the place you suggested with no luck. I also tried in a couple different places with no luck.

Must be something different about your code. The consume worked for me.
Try my code. Maybe I didn't understand how you are using it or what you want to happen,

/*
When a user Scans a barcode representing a # sign while focus is on the fromComboBox, code is used
to scroll through the list. It scrolls through the list fine, but then it moves to the next field
on the screen, because the scanner (that is what we use for input) is setup to put a return character
at the end of each scan (required).
Is there a way to stop/cancel the return character from being read/processed?
Below is a small sample program that demos the problem. You can run it and select
one of the items in the combo. Then when I do a scan of the barcode to scroll, it scrolls, then moves
to the next field (It should stay on the the combobox).
*/
//package testb;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/*
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
*/
//VS4E -- DO NOT REMOVE THIS LINE!
public class TestCancelKeyStroke extends JFrame {
private static final long serialVersionUID = 1L;
private JComboBox fromComboBox;
private JTextField jTextField0;
private JButton okButton;
private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
public TestCancelKeyStroke() {
initComponents();
}
private void initComponents() {
// setLayout(new GroupLayout());
add(getJComboBox0(), BorderLayout.NORTH); //, new Constraints(new Leading(83, 148, 10, 10), new Leading(56, 10, 10)));
add(getJTextField0(), BorderLayout.CENTER);//, new Constraints(new Leading(85, 136, 10, 10), new Leading(109, 29, 10, 10)));
add(getJButton0(), BorderLayout.SOUTH);//, new Constraints(new Leading(105, 10, 10), new Leading(185, 10, 10)));
setSize(320, 240);
}
private JButton getJButton0() {
if (okButton == null) {
okButton = new JButton();
okButton.setText("OK");
okButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
// okButtonMouseMouseClicked(event);
}
});
}
return okButton;
}
private JTextField getJTextField0() {
if (jTextField0 == null) {
jTextField0 = new JTextField();
jTextField0.setText("jTextField0");
}
return jTextField0;
}
private JComboBox getJComboBox0() {
if (fromComboBox == null) {
fromComboBox = new JComboBox();
fromComboBox.setEditable(true);
fromComboBox.setModel(new DefaultComboBoxModel(new Object[] { "" }));
fromComboBox.setDoubleBuffered(false);
fromComboBox.setBorder(null);
fromComboBox.addItem("oranges");
fromComboBox.addItem("lemons");
fromComboBox.addItem("limes");
fromComboBox.getEditor().getEditorComponent().
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent event) {
fromComboBoxKeyKeyPressed(event);
}
});
}
return fromComboBox;
}
private static void installLnF() {
try {
String lnfClassname = PREFERRED_LOOK_AND_FEEL;
if (lnfClassname == null)
lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(lnfClassname);
} catch (Exception e) {
System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
+ " on this platform:" + e.getMessage());
}
}
/**
* Main entry of the class.
* Note: This class is only created so that you can easily preview the result at runtime.
* It is not expected to be managed by the designer.
* You can modify it as you like.
*/
public static void main(String[] args) {
installLnF();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TestCancelKeyStroke frame = new TestCancelKeyStroke();
frame.setDefaultCloseOperation(TestCancelKeyStroke.EXIT_ON_CLOSE);
frame.setTitle("TestCancelKeyStroke");
frame.getContentPane().setPreferredSize(frame.getSize());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
//Here is the problem area. When a user Scans a barcode representing a # sign
//while focus is on the fromComboBox, this code is used to scroll through the
//list. It scrolls through the list fine, but then it moves to the next field
//on the screen. because the scanner (that is what we use for input) is setup
//to put a return character at the end of each scan (required). Is there a way
//to stop/cancel the return from being read?
private void fromComboBoxKeyKeyPressed(KeyEvent e) {
final int num = fromComboBox.getItemCount();
Boolean bScroll = new Boolean(false);
//this part runs after the scroll
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
//enter key
e.consume(); // Get rid of the Enter
if (bScroll.equals(true)) {
fromComboBox.requestFocus();
bScroll = false;
}else {
jTextField0.requestFocus();
}
}else if ((e.getKeyCode() == '3') && (e.isShiftDown())){
//use # to scroll through From dropdown
//runs through here fine
bScroll = true;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (fromComboBox.getItemAt(num - 1).equals(fromComboBox.getSelectedItem())) {
fromComboBox.setSelectedIndex(0);
}else{
fromComboBox.setSelectedIndex(fromComboBox.getSelectedIndex() + 1);
}
}
});
}
}
}

Copied your code and still have the same issue. So what I need to happen:

1. Once app starts, manually select an item such as Oranges.
2. Using my scan gun, I scan the # sign barcode (which will include a carriage return at the end).
3. The fromComboBox should scroll to the "Lemons" and the cursor should remain on the fromComboBox.

That is it. So if I could somehow cancel out that carriage return I think it would work. At least in vb I can do a KeyCode = 0 to cancel it.

Yes, the current issue is focus. Focus should remain on the combobox, but with the return key it moves to the next field on the screen. I need to have the option of moving to the next field with a return key though. Like if the user just hits the "enter" key (scans the Enter barcode).

I was able to resolve my issue above by adding some boolean checks in the fromComboBoxLostFocus event. This event was setting focus to the next textfield.

Thanks everyone for your help.

Now my next issue. Kind of relates back to my first one. If I am on the fromComboBox and click on a barcode that should Tab to the next field, the character used to Tab ">" overwrites the text in the fromComboBox (text in fromComboBox is highlighted when the Tab is scanned). I tried to utilize the below but did not work. I added it below the code }else if ((e.getKeyCode() == '3') && (e.isShiftDown())){

So how does the code know how the Enter/Return key was pressed?
If the scan gun, then keep the focus on the combo box.
If a user, move to next field.

The scan gun basically emulates keys being pressed when scanning a barcode. So using the Action Listener, you can capture the keycode, just like on a keyboard. I don't know how you would be able to differeniate b/w a person and scan gun.

If both can send a Enter key stroke and you want to ignore the one from the scan gun and you "don't know how you would be able to differeniate b/w a person and scan gun", I can't think of any solution to your problem.

If both can send a Enter key stroke and you want to ignore the one from the scan gun and you "don't know how you would be able to differeniate b/w a person and scan gun", I can't think of any solution to your problem.

OK, thanks for trying to assist though. You have helped me out a lot. I just thought there might be a way to "cancel" a keystroke like in VB.

I was able to figure out the initial problem. I posted another in here though sort of related to the first problem you helped with. I tried using the e.consume and the InvokeLater method but wasn't able to get it working correctly. Any thoughts on this one?