I need your thoughts

so im trying to practice oop with a onscreen keyboard and my functions and buttons are working as intended. but i dont think im doing it correctly simply bc im not using class or constructor, so i did it this way, is this still considered oop. Btw its not finished i just added functionality to few keys for testing. so do i need to use classes and constructors for it to be considered oop?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Open KeyBoard</title>
    <link rel="stylesheet" href="styles.css"/>
</head>
<body>
    <div id="input"></div>
    <div id="KeyBoard">
        <button id="key1" class="keys">`</button>
        <button id="key2" class="keys">1</button>
        <button id="key3" class="keys">2</button>
        <button id="key4" class="keys">3</button>
        <button id="key5" class="keys">4</button>
        <button id="key6" class="keys">5</button>
        <button id="key7" class="keys">6</button>
        <button id="key8" class="keys">7</button>
        <button>8</button>
        <button>9</button>
        <button>0</button>
        <button>-</button>
        <button>=</button>
        <button id="backspace">backspace</button>
        <button id="tab">tab</button>
        <button>q</button>
        <button>w</button>
        <button>e</button>
        <button>r</button>
        <button>t</button>
        <button>y</button>
        <button>u</button>
        <button>i</button>
        <button>o</button>
        <button>p</button>
        <button>[</button>
        <button>]</button>
        <button>\</button>
        <button id="caps">caps</button>
        <button>a</button>
        <button>s</button>
        <button>d</button>
        <button>f</button>
        <button>g</button>
        <button>h</button>
        <button>j</button>
        <button>k</button>
        <button>l</button>
        <button>;</button>
        <button>'</button>
        <button id="enter">enter</button>
        <button class="shift">shift</button>
        <button>z</button>
        <button>x</button>
        <button>c</button>
        <button>v</button>
        <button>b</button>
        <button>n</button>
        <button>m</button>
        <button>,</button>
        <button>.</button>
        <button>/</button>
        <button class="shift">shift</button>
        <button>.com</button>
        <button>@</button>
        <button id="space"></button>
    </div>
    <script src="script.js"></script>   
</body>
</html>
 const button = document.getElementsByClassName('keys');
 const input = document.getElementById('input');
 const keys = [
{
    name: '`',
    cap: '~',
    id: 0,
    keybind: document.getElementById('key1'),
    pressed(){
         this.keybind.addEventListener('click', () =>{
            input.innerHTML += '`';
         })
    }
},
{
    name: '1',
    cap: '!',
    id: 1,
    keybind: document.getElementById('key2'),
    pressed(){
         this.keybind.addEventListener('click', () =>{
            input.innerHTML += '1';
         })
    }
},
{
    name: '2',
    cap: '@',
    id: 2,
    keybind: document.getElementById('key3'),
    pressed(){
         this.keybind.addEventListener('click', () =>{
            input.innerHTML += '2';
         })
    }
},
{
    name: '3',
    cap: '#',
    id: 3,
    keybind: document.getElementById('key4'),
    pressed(){
         this.keybind.addEventListener('click', () =>{
            input.innerHTML += '3';
         })
    }
},
{
    name: '4',
    cap: '$',
    id: 4,
    keybind: document.getElementById('key5'),
    pressed(){
         this.keybind.addEventListener('click', () =>{
            input.innerHTML += '4';
         })
    }
},
{
    name: '5',
    cap: '%',
    id: 5,
    keybind: document.getElementById('key6'),
    pressed(){
         this.keybind.addEventListener('click', () =>{
            input.innerHTML += '5';
         })
    }
},
{
    name: '6',
    cap: '^',
    id: 6,
    keybind: document.getElementById('key7'),
    pressed(){
         this.keybind.addEventListener('click', () =>{
            input.innerHTML += '6';
         })
    }
},
{
    name: '7',
    cap: '&',
    id: 7,
    keybind: document.getElementById('key8'),
    pressed(){
         this.keybind.addEventListener('click', () =>{
            input.innerHTML += '7';
         })
    }
},
    ];

keys.forEach((key) =>{
   key.pressed()
})
   
 
#KeyBoard{
    border:  1px solid black;
    width: 48vw;
    height: 40%;
}
button{
    width: 50px;
    height: 40px;
    margin: 4px;
}
#input{
    border: 1px solid black;
    width:100vw;
    height: 50vh;
}
#backspace{
    width: 100px;
}
#tab{
    width: 100px;
}
#caps{
    width: 120px;
}
#enter{
    width: 90px;
}
.shift{
    width: 120px;
}
#space{
    width: 85%;
}

its not oop. in oop you model a real world things, like car and using inheritance like sub class of car could electric car and using methods in those classes like start-engine stop-engine. brakes, accelerate etc and for attributes like color, two-door, four-door. yes you need to use class and constructors

oop needs for functionatlity, vision and principles for being modern developer. you can use your code like this, or with even more codes. but imagine that you re a department with 10 people and you and your team working on developing some project and project has many lines of codes and every day you re trying new features. if you keep your code without using oop it would be hard to find what you want ( as codes with interact each other ). so i hope you get what i mean

Ya I understand, but I heard one way in practicing pop building a onscreen keyboard Ig I don’t have inhertence but I do have amethod

Ya I know but just curious the basics of it bc I’m still struggling to get a grasps at it and heard onscreen keyboard could be good practice

i could not understand what you want to say

I meant I heard building a onscreen keyboard could help with practicing oop

you can apply oop for every project, onscreen keyboard project too

Can give tips on how I can go about my project in a oop manner

need help i dont know how to actually do this in oop

const button = document.getElementsByClassName('keys');
 const input = document.getElementById('input');

 const keys = {
  keybinds:  [
{
    name: '`',
    cap: '~',
    id: 0,
    keybind: document.getElementById('key1'),
},
{
    name: '1',
    cap: '!',
    id: 1,
    keybind: document.getElementById('key2'),
},
{
    name: '2',
    cap: '@',
    id: 2,
    keybind: document.getElementById('key3'),
},
{
    name: '3',
    cap: '#',
    id: 3,
    keybind: document.getElementById('key4'),
},
{
    name: '4',
    cap: '$',
    id: 4,
    keybind: document.getElementById('key5'),
},
{
    name: '5',
    cap: '%',
    id: 5,
    keybind: document.getElementById('key6'),
},
{
    name: '6',
    cap: '^',
    id: 6,
    keybind: document.getElementById('key7'),
    
},
{
    name: '7',
    cap: '&',
    id: 7,
    keybind: document.getElementById('key8'),
},
    ],
    press(key){
        key.keybind.addEventListener('click', ( ) =>{
            input.innerHTML += key.name;
        })
    },
    each(){
        this.keybinds.forEach((key) =>{
             this.press(key);
           })
    }
};
keys.each();
 
 

Could write a bit about the expected goal? How this class would be used?

1 Like

I’m trying to make a onscreen keyboard with oop

Right, but how would you like to structure it? What exactly would be the class for - each key, whole keyboard?

You can think of what thing in your keyboard is being repeated in terms of behaviour? What things act the same way? What things looks the same way to your app user? What things are handled the same way in your code?

Once you know that, you can create classes.

Classes are just ways to group things that are the same. If I was making an app for a pharmacy for eg, I may notice that drugs all have names and amounts. So maybe I need a class for drugs. I may also notice that the companies that make the drugs all have names and addresses, so maybe I need another class for manufacturers.

Just think about the app you made in terms of things that act the same or are treated the same and you can maybe figure out the classes that way.

1 Like

The point of classes isn’t that much different from functions.

You can feed it data as you instantiate it, use methods on the data, use getters and setters to better control it. Basically, you make it reusable like a function.

You could feed it symbols, numbers, selector strings, etc., and have it set up everything based on the data you provide it. Then reuse it with other data as needed.

1 Like