How can i ask and save the mac address in a String?

what library do i need ? what is the command ?. Thank you all !

No libraries needed. Just use text().

boolean done = false;
String mac = "";

void setup(){
  size(400,400);
}

void draw(){
  background(0);
  fill(255);
  text("Please type in your MAC address.", 20, 20);
  text("Press enter when you are done.", 20, 40);
  fill(done?0:255,255,0);
  text(mac, 20, 60);
  if(done){
    fill(0,0,255);
    text("Thank you.", 20, 80);
  }
}

void keyPressed(){
  if( mac.length() > 0 && (keyCode == BACKSPACE || keyCode == DELETE) ){
    mac = mac.substring(0,mac.length()-1);
  }
  if( keyCode == ENTER || keyCode == RETURN ){
    done = true;
  }
  if( !done ){
    if( key >= 'a' && key <= 'f' ){
      mac = mac + char( key - 'a' + 'A' );
    }
    if( key >= 'A' && key <= 'F' ){
      mac = mac + char( key );
    }
    if( key >= '0' && key <= '9' ){
      mac = mac + char( key );
    }
  }
}

Okay, okay. That was mean of me. A little Google search turned up enough code to do it in Java mode. You’re probably out of luck in JavaScript modes though, for security reasons.

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

void setup() {
  size(200,200);
  InetAddress ip;
  try {
    ip = InetAddress.getLocalHost();
    //println("Current IP address : " + ip.getHostAddress());
    NetworkInterface network = NetworkInterface.getByInetAddress(ip);
    byte[] mac = network.getHardwareAddress();
    print("Current MAC address : ");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < mac.length; i++) {
      sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
    }
    System.out.println(sb.toString());
  } 
  catch (UnknownHostException e) {

    e.printStackTrace();
  } 
  catch (SocketException e) {

    e.printStackTrace();
  }
}

void draw() {
  background(0);
  exit();
}

YMMV

4 Likes

I had already seen that code but I didn’t know I could type it in processing. Thank youuuu !