# Open a Link on a browser with keyPressed

**URL:** <https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740>\
**Category:** Coding Questions\
**Created:** [December 18, 2018, 3:57pm UTC](https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740 "2018-12-18T15:57:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_megss0](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/_megss0/32/2853_2.png) [@\_megss0](https://discourse.processing.org/u/_megss0)\
**Post date:** [December 18, 2018, 3:57pm UTC](https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740/1 "2018-12-18T15:57:21Z")

</div>

i want to open a new tab with a link but using the fuction keyPressed  
something like; if i press CTRL+A open a tab on the browser with a specif link

---

<div class="post-metadata">

**Author:** ![figraham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/figraham/32/4161_2.png) [@figraham](https://discourse.processing.org/u/figraham)\
**Post date:** [December 18, 2018, 4:59pm UTC](https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740/2 "2018-12-18T16:59:24Z")

</div>

I suggest using the [Java Desktop](https://docs.oracle.com/javase/8/docs/api/java/awt/Desktop.html) class. Here’s an example:

```auto
import java.awt.Desktop;
import java.net.URI;
import java.io.IOException;
import java.net.URISyntaxException;

Desktop desktop;
boolean controlPressed = false;

void setup() {
  size(200, 200);
  desktop = Desktop.getDesktop();
}

void draw() {
  // must keep window running to register keys
}

void keyPressed() {
  if (keyCode == 17) { // Control
    controlPressed = true;
  }
  if (keyCode == 65 && controlPressed) { // A
    openWebPage("https://discourse.processing.org/");
  }
}

void keyReleased() {
  if (keyCode == 17) { // Control
    controlPressed = false;
  }
}

void openWebPage(String siteUrl) {
  try {
    URI site = new URI(siteUrl);
    if (Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {
      desktop.browse(site);
    } else {
      println("Desktop does not support browse");
    }
  } catch(URISyntaxException e) {
    e.printStackTrace();
  } catch(IOException e) {
    e.printStackTrace();
  }
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 18, 2018, 5:06pm UTC](https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740/3 "2018-12-18T17:06:44Z")

</div>

> [@dan850](#):
>
> Please provide a brief description of the links.

Links to **link()** reference:

[ProcessingJS.org/reference/link\_/](http://ProcessingJS.org/reference/link_/)

[Processing.GitHub.io/processing-javadocs/everything/processing/core/PApplet.html#link-java.lang.String-](http://Processing.GitHub.io/processing-javadocs/everything/processing/core/PApplet.html#link-java.lang.String-)

---

<div class="post-metadata">

**Author:** ![\_megss0](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/_megss0/32/2853_2.png) [@\_megss0](https://discourse.processing.org/u/_megss0)\
**Post date:** [December 18, 2018, 6:37pm UTC](https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740/4 "2018-12-18T18:37:08Z")

</div>

thank you so much, this is really helpfull!!! i have to do this like 5 times in my project, do you know a “simpler” way to do this? thanks again 😛

---

<div class="post-metadata">

**Author:** ![figraham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/figraham/32/4161_2.png) [@figraham](https://discourse.processing.org/u/figraham)\
**Post date:** [December 18, 2018, 7:18pm UTC](https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740/5 "2018-12-18T19:18:31Z")

</div>

You could encapsulate it in a class. Which is just good practice.

```auto
BrowserOpener browserOpener;

void setup() {
  size(200, 200);
  browserOpener = new BrowserOpener();
  // A
  browserOpener.addSite(65, "https://discourse.processing.org/");
  // S
  browserOpener.addSite(83, "https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740");
}

void draw() {
  // must keep window running to register keys
}

void keyPressed() {
  browserOpener.keyDown(keyCode);
}

void keyReleased() {
  browserOpener.keyUp(keyCode);
}

```

```auto
import java.awt.Desktop;
import java.net.URI;
import java.io.IOException;
import java.net.URISyntaxException;

class BrowserOpener {
  
  ArrayList<SiteKey> siteKeys;
  boolean controlPressed;
  Desktop desktop;
  
  BrowserOpener() {
    siteKeys = new ArrayList<SiteKey>();
    controlPressed = false;
    desktop = Desktop.getDesktop();
  }
  
  void addSite(int codeForKey, String site) {
    siteKeys.add(new SiteKey(codeForKey, site));
  }
  
  void keyDown(int codeForKey) {
    if (codeForKey == 17) {
      controlPressed = true;
    } else if (controlPressed) {
      for (int i = 0; i < siteKeys.size(); i++) {
        if (codeForKey == siteKeys.get(i).codeForKey) {
          openSite(i);
        }
      }
    }
  }
  
  void keyUp(int codeForKey) {
    if (codeForKey == 17) {
      controlPressed = false;
    }
  }
  
  void openSite(int index) {
    try {
      URI site = new URI(siteKeys.get(index).site);
      if (Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {
        desktop.browse(site);
      } else {
        println("Desktop does not support browse");
      }
    } catch(URISyntaxException e) {
      e.printStackTrace();
    } catch(IOException e) {
      e.printStackTrace();
    }
  }
}

class SiteKey {
  int codeForKey;
  String site;
  SiteKey(int codeForKey, String site) {
    this.codeForKey = codeForKey;
    this.site = site;
  }
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [December 21, 2018, 12:27am UTC](https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740/6 "2018-12-21T00:27:53Z")

</div>

@figraham, what are important differences between using `link()` and what you can do with the `java.awt.Desktop` approach?

---

<div class="post-metadata">

**Author:** ![dan850](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@dan850](https://discourse.processing.org/u/dan850)\
**Post date:** [December 21, 2018, 1:45am UTC](https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740/7 "2018-12-21T01:45:10Z")

</div>

Please provide a brief description of the links.

---

<div class="post-metadata">

**Author:** ![figraham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/figraham/32/4161_2.png) [@figraham](https://discourse.processing.org/u/figraham)\
**Post date:** [December 21, 2018, 9:47pm UTC](https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740/8 "2018-12-21T21:47:58Z")

</div>

Apologizes I thought `link()` was only implemented within Processing.js because that’s the only place I saw it in documentation. Just saw @GoToLoop’s link to the javadocs. I’d say there’s basically no point to using `Desktop` over link. Would recommend that @_megss0 replaces the `openWebPage()` method with the `link()` method.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 21, 2018, 10:07pm UTC](https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740/9 "2018-12-21T22:07:03Z")

</div>

> [@figraham](#):
>
> … I thought `link()` was only implemented within Processing.js…

I’ve posted **link()** as an alternative answer to your Desktop-based solution.

Obviously I had to use Pjs’ reference instead b/c Processing’s arbitrarily hides lotsa good API from us!

The fact it was a Pjs link shouldn’t be interpreted as being exclusive to it, since I gave it as a Java Mode answer!

Though I remember **link()** was once part of Processing’s reference by the time of P1.5.1.

Later on, I remembered to include a Processing JavaDocs reference too.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [December 21, 2018, 11:27pm UTC](https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740/10 "2018-12-21T23:27:55Z")

</div>

Well, one good argument for the standalone approach is that link is semi-documented / semi-hidden – which I suppose means it is always possible that it could be removed in the future. Certainly, not finding it in the reference can be confusing for beginners – although I’m honestly happy that it isn’t widely used by beginners; it could be frustrating if people were frequently embedding the equivalent of pop-ups in their sketches.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 21, 2018, 11:50pm UTC](https://discourse.processing.org/t/open-a-link-on-a-browser-with-keypressed/6740/11 "2018-12-21T23:50:04Z")

</div>

> [@jeremydouglass](#):
>
> which I suppose means it is always possible that it could be removed in the future.

That’d be an unfortunate, shortsighted, arbitrary decision then. 🤡  
This **link()** subject keeps coming from time to time to our Processing forum: 🔗

- [Tagged with index.html - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussions/tagged?Tag=link%28%29)

Its Desktop replacement is a tad too complex for Processing’s target audience. 😐
