If I understand correctly what you're saying, the problem is that when you pick up a new piece it takes a while to get the correct key signature under your fingers.
In learning "all the notes in any major or minor scales", it's a bit like you've learned the alphabet, or learned the rules of a particular game. The way to apply this, as Mordent says, is to practise. But you have to practise properly.
This is why there are studies and études (yes, tautology) as well as actual performance pieces. Studies are written to help developing musicians with a certain skill. Now, a skill is more than just having your fingers in the the right place at the right time. If you have a study in F Major, you need to have in mind to play that set of notes including B flat. The thought process is part of it. The first thing that happens is yo learn to blitz an F Major study. The next one in F Major will be learned faster, and the third one you'll practically sight read.
Then you go on to the next "level" or element that you have to learn (in your case, the next key), and repeat the process. Then again, and again. The thing is, as you repeat the process, not only do you learn the pieces themselves, but you'll get faster at learning them. You get better at getting better because your brain is learning how to learn. The word to describe it is "experience".
In the case of playing in a certain key, you'll soon see the key signature and, instead of sweating about jumping on every flat as soon as you see it, you'll have what I call a low-level hum in the back of your mind that reminds you to go to that note. You think in terms of that key; remember that there are other notes in D Major apart from F sharp and C sharp.
This is a general description, but it's broadly what happens when you learn any fine skill. As mentioned, a qualified teacher will be able to identify any gaps in your foundation and go from there.