为什么两个文本字段中的逗号同时放置而不聚焦? [英] Why is the comma in two text fields put at the same time without focusing?

查看:67
本文介绍了为什么两个文本字段中的逗号同时放置而不聚焦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在按下逗号时更改代码中的条件?这样打字时逗号不会同时出现在两个文本框 1 和 2 中,而是分开出现?这些文本字段中的字符限制也会发生同样的情况.如何在它们之间拆分两个文本字段的处理!为什么在没有聚焦文本字段的情况下放置逗号?照片:完整代码:

How can I change conditions in code when comma is pressed? So that the comma does not appear simultaneously in two text boxes 1 and 2 when typing, but separately? The same thing happens with the limitation of characters in these text fields. How to split the processing of two text fields among themselves! Why is the comma placed without focusing text fields? Foto: Complete code:

import controlP5.*;

ControlP5 cp5;
Textfield X9;
Textfield X10;

void setup() {
  size(700,400);
  
  PFont font = createFont("arial",20);
  
  cp5 = new ControlP5(this);
  
  X9 = cp5.addTextfield("1")
     .setPosition(20,100)
     .setSize(200,40);
     X9.setInputFilter(ControlP5.INTEGER)
     .setFont(font)
     .setAutoClear(false)
     .setColor(color(255,0,0))
     ;
                 
  X10 = cp5.addTextfield("2")
     .setPosition(20,170)
     .setSize(200,40);
     X10.setInputFilter(ControlP5.INTEGER)
     .setFont(createFont("arial",20))
     .setAutoClear(false)
     ;
     
  textFont(font);}
void draw() {
  background(0);
  fill(255);}
public void keyPressed(KeyEvent e) {
  if (e.getKey() == ','){
  X9.setText(X9.getText() + ',');}
  {
//  if(X10.getText().length()>=4) { X10.setText(X10.getText().substring(0, 3));}
  {
if (e.getKey() == ','){
  X10.setText(X10.getText() + ',');}
  {
 // if(X9.getText().length()>=4) { X9.setText(X9.getText().substring(0, 3));}
  }}}
}

推荐答案

为什么在没有聚焦文本字段的情况下放置逗号?

Why is the comma placed without focusing text fields?

因为您正在使用全局 keyPressed() 事件.此条件: if (e.getKey() == ',') 检查 , 键是否被按下,并且在您的情况下检查两次是多余的.它相当于这个更简单/更清晰的代码段:

Because you are using the global keyPressed() event. This condition: if (e.getKey() == ',') checks that the , key is pressed and it's redundant to check twice in your case. It's equivalent to this simpler/cleaner snippet:

public void keyPressed(KeyEvent e) {
  if (key == ','){
    X9.setText(X9.getText() + ',');
    X10.setText(X10.getText() + ',');
  }
}

没有任何检查字段是否聚焦.

There isn't any check if a field is focused or not.

你的意思可能是这样的?:

You probably meant something like this ?:

public void keyPressed(KeyEvent e) {
  if (key == ',') {
    if (X9.isFocus()) {
      X9.setText(X9.getText() + ',');
    }
    if (X10.isFocus()) {
      X10.setText(X10.getText() + ',');
    }
  }
}

总的来说,主要目标是什么还不清楚.也许有更简单的方法来实现它?

Overall it's unclear what the main goal is. Perhaps there's a simpler way to achieve it ?

请记住,您可以通过 void controlEvent(ControlEvent event) 监听单个 controlP5 组件事件.参见处理>例子 >贡献的图书馆 >控制P5>控制器 >ControlP5Textfield 一个不错的演示.

Remeber that you can listen for individual controlP5 component events via void controlEvent(ControlEvent event). See Processing > Examples > Contributed Libraries > ControlP5 > controllers > ControlP5Textfield for a nice demo.

此外,我建议格式化/保持代码整洁.这可能是一个很快就扔掉的草图,但建立一个好的习惯会得到回报,因为你会花更多的时间阅读代码而不是编写代码.随着您的程序变得越来越大,您会希望让未来的自己更轻松:)

Additionally I recommend formatting/keeping code tidy. It may be a quick throw-away sketch, but buiilding a good habbit will pay off as you'll spend way more time reading code than writing code. As your programs become larger and larger you'll want to make life easier for your future self :)

更新根据您的评论,这里是一个最小草图,它应该允许您控制 7.4 和 16.8 之间的浮点数和 1800-1900 之间的整数:

Update Based on your comments here is a minimal sketch that should allow you control a floating point number between 7.4 and 16.8 and an integer number between 1800-1900:

import controlP5.*;

ControlP5 cp5;

int serialInt = 1800;

float serialFloat = 7.4;

void setup() {
  size(300,300);
  noStroke();
  cp5 = new ControlP5(this);
  
  cp5.addNumberbox("serialInt")           // notice the component name matches the variable name: controlP5 links the two for you
     .setBroadcast(false)                 // disable events while we update value specific properties 
     .setPosition(100,100)                // screen location
     .setSize(100,20)                     // screen dimensions
     .setMultiplier(10)                   // set the sensitifity of the numberbox: each step is 10
     .setDirection(Controller.HORIZONTAL) // change the control direction to left/right
     .setRange(1800, 9000)                // set minimum, maximum value
     .setBroadcast(true)                  // enable events (after setting range)
     ;
  

  cp5.addNumberbox("serialFloat")
     .setBroadcast(false)                 // disable events while we update value specific properties
     .setPosition(100,140)                // screen location
     .setSize(100,20)                     // screen dimensions
     .setMultiplier(0.01)                 // set the sensitifity of the numberbox: each step is 0.01
     .setDirection(Controller.HORIZONTAL) // change the control direction to left/right
     .setRange(7.4,16.8)                  // set minimum, maximum value
     .setBroadcast(true)                  // enable events (after setting range)
     ;
  
}
void draw() {
  background(0);
}
// gets called whenever a component updates value
void controlEvent(ControlEvent event){
  println(event.getController().getName(),"changed value to",event.getValue(),"serialInt = ",serialInt,"serialFloat = ",serialFloat);
}

单击并水平拖动以更改值.请注意 ControlP5 提供的一些有用的东西:

Click and drag horizontally to change values. Notice a few helpful things that ControlP5 provides:

  • 如果您将控制器命名为与控制器相同的名称,则两者会自动连接:节省大量时间.(如果不能使用变量安全名称,可以查看Examples > Contributed Examples > ControlP5 > use > ControlP5plugTo)
  • 您可以轻松设置所需的范围和精度
  • 您可以选择使用 controlEvent() 来判断值何时更改
  • if you name your controller the same name as the controller the two are connected automatically: huge time saver. (If you can't use variable safe name you can look at Examples > Contributed Examples > ControlP5 > use > ControlP5plugTo)
  • you can easily set the range and precision desired
  • you can use controlEvent() optionally to tell when a value changed

关于串行数据:

  • 7.4 到 16.8 范围很容易,如果您只需要 0.1 精度:只需将值乘以 10 即可得到适合单个字节(0-255 范围)的 74 到 168
  • 1800 到 9000 的范围比较棘手,因为 9000-1800 = 7200 步.对于这种精度,您至少需要 13 位(2 ^ 13 = 8192,因此可以容纳 7200 个值).您可能需要使用等效于 highByte()lowByte() 发送前正在处理和 word() 在 Arduino(或等效的 STM32 开发工具,如果不使用 Arduino).

例如:

void serialWriteWord(Serial port,int value){
  port.write(highByte(value));
  port.write(lowByte(value));
}

byte lowByte(int word){
  return (byte)(word & 0xff);
}

byte highByte(int word){
  return (byte)(word >> 8);
}

串行通信部分是一个完全独立的问题(另一个问题)

The serial communication part is a totally separate issue (for another question)

这篇关于为什么两个文本字段中的逗号同时放置而不聚焦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆