Skip to content

Commit 14fb227

Browse files
committed
Add copy button to the serial monitor
Add a copy button to the serial monitor that copies the text in the serial monitor output area to the system clipboard.
1 parent a87024d commit 14fb227

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

app/src/processing/app/AbstractTextMonitor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
3838
protected JTextField textField;
3939
protected JButton sendButton;
4040
protected JButton clearButton;
41+
protected JButton copyButton;
4142
protected JCheckBox autoscrollBox;
4243
protected JCheckBox addTimeStampBox;
4344
protected JComboBox<String> lineEndings;
@@ -84,6 +85,7 @@ public void windowGainedFocus(WindowEvent e) {
8485

8586
sendButton = new JButton(tr("Send"));
8687
clearButton = new JButton(tr("Clear output"));
88+
copyButton = new JButton(tr("Copy output"));
8789

8890
upperPane.add(textField);
8991
upperPane.add(Box.createRigidArea(new Dimension(4, 0)));
@@ -131,6 +133,8 @@ public void windowGainedFocus(WindowEvent e) {
131133
pane.add(Box.createRigidArea(new Dimension(8, 0)));
132134
pane.add(serialRates);
133135
pane.add(Box.createRigidArea(new Dimension(8, 0)));
136+
pane.add(copyButton);
137+
pane.add(Box.createRigidArea(new Dimension(8, 0)));
134138
pane.add(clearButton);
135139

136140
applyPreferences();
@@ -143,6 +147,7 @@ protected void onEnableWindow(boolean enable)
143147
{
144148
textArea.setEnabled(enable);
145149
clearButton.setEnabled(enable);
150+
copyButton.setEnabled(enable);
146151
scrollPane.setEnabled(enable);
147152
textField.setEnabled(enable);
148153
sendButton.setEnabled(enable);
@@ -161,6 +166,10 @@ public void onClearCommand(ActionListener listener) {
161166
clearButton.addActionListener(listener);
162167
}
163168

169+
public void onCopyCommand(ActionListener listener) {
170+
copyButton.addActionListener(listener);
171+
}
172+
164173
public void onSerialRateChange(ActionListener listener) {
165174
serialRates.addActionListener(listener);
166175
}

app/src/processing/app/SerialMonitor.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import processing.app.legacy.PApplet;
2323

2424
import java.awt.Color;
25+
import java.awt.Toolkit;
26+
import java.awt.datatransfer.Clipboard;
27+
import java.awt.datatransfer.StringSelection;
2528
import java.awt.event.ActionEvent;
2629

2730
import static processing.app.I18n.tr;
@@ -57,8 +60,14 @@ public SerialMonitor(Base base, BoardPort port) {
5760
send(textField.getText());
5861
textField.setText("");
5962
});
60-
63+
6164
onClearCommand((ActionEvent event) -> textArea.setText(""));
65+
66+
onCopyCommand((ActionEvent event) -> {
67+
String text = textArea.getText();
68+
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
69+
clipboard.setContents(new StringSelection(text), null);
70+
});
6271
}
6372

6473
private void send(String s) {

0 commit comments

Comments
 (0)